java - Firebase 节点更新时接收通知

标签 java android firebase-realtime-database notifications

我有 2 个应用程序,一个是客户预订应用程序,一个是管理员接收应用程序。它们都连接到同一个 Firebase 数据库。当客户进行预订时,我可以在我的管理应用程序中查看此信息。但是,如何才能在客户应用程序中进行预订后在管理应用程序中收到通知?

我找到了这段代码,但我如何实现它,以便即使管理应用程序未打开也显示通知?

 Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(subject)
            .setContentText(object.getString("body"))
            .setAutoCancel(true)
            .setSound(notificationSoundURI)
            .setContentIntent(resultIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, mNotificationBuilder.build());

    ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, ToneGenerator.MAX_VOLUME);
    toneG.startTone(ToneGenerator.TONE_CDMA_HIGH_L, 3000);
    ((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(2000);

编辑

我的 Firebase 树如下所示

{
"Bookings",
"UserID Appears here",
"User booking info appears here"}
}

预订节点是一个常量并且始终存在,一旦进行预订,用户 ID 就会出现。我是否可以拥有某种即使在应用程序关闭时也能运行的服务来监听“UserID”节点的更新?然后触发上面的通知方法?我从未使用过通知和服务。

最佳答案

您的管理应用程序的代码

// Minimal Fcm Service
class FcmService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        remoteMessage.data.isNotEmpty().let {
            // data message, handle it or start a service
            Intent(this, ConnectionService::class.java).also { intent -> startService(intent) }
        }

        remoteMessage.notification?.let {
        // notification message, you can define your custom notification here or just leave it that way
        }
    }
}

这就是您在 list 中注册它的方式

<service
    android:name="com.yourpackage.appname.FcmService"
    android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>

编辑:

Notification message:

FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

Data message:

Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

编辑2:Java代码

public class FcmService extends FcmService {
    @Override
    public void onMessageReceived(@NotNull RemoteMessage remoteMessage) {
        if (!remoteMessage.getData().isEmpty()){
            // data message
            // start a service or handle it the way you want
        }
        
        if (remoteMessage.getNotification() != null){
            // notification message
        }
    }
}

Link to Official Fcm Service in java

关于java - Firebase 节点更新时接收通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59919988/

相关文章:

android - 在 Firebase 模型中保存推送的 ID

angularjs - 是否可以在 firebase 中获取唯一值的数组?

android - NFC 中的 AAR 记录 : Where's The Payload?

android - 在 Android 应用程序中使用带有 ProGuard 的 SqlCipher 时出现 NoSuchFieldError

java - Spring bean,XmlWebApplicationContext 中的生命周期(Web 上下文)

java - Drools 规则中的映射和字符串

java - 如何检查 ArrayMap.keySet() 是否包含某个变量+正则表达式?

swift - 为什么 Firebase 在应用程序崩溃时不支持 onDisconnect?

java - 如何从 Firestore 获取我的模型 (POJO) 类的对象?

java - MyBatis 完整注释配置以检索 OUT 参数中的存储过程结果?