在 Firebase 节点更新时接收通知

Receiving a notification when Firebase node updates

我有 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"}
}

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

这是 Cloud Functions and Firebase Cloud Messaging. In fact, this scenario is so common that there's an example of it in the Firebase documentation called: notify users when something interesting happens 的典型用例:

Cloud Functions 允许您通过 Google 服务器上的 运行 段 (Node.js) 代码响应 Firebase 项目中的事件。由于此代码在 Google 的服务器上运行,因此即使应用程序未处于活动状态,它也会处于活动状态。然后这段代码可以使用 Firebase Admin SDK 来调用其他 Firebase 服务,例如这里的 Cloud Messaging。

Firebase Cloud Messaging 允许您向安装在设备上的应用程序发送消息,即使该应用程序未被主动使用。您通常会使用问题中的代码来响应收到此类消息,然后在该设备上显示本地通知。然后当用户点击通知时,您打开应用程序,并使用实时数据库 API 从服务器读取(其余)数据

有关这方面的更多信息,另请参阅:

您的管理应用代码

// 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
        }
    }
}

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

<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.

Edit2: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