Firebase节点更新时接收通知 [英] Receiving a notification when Firebase node updates

查看:46
本文介绍了Firebase节点更新时接收通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个应用程序,一个客户预订应用程序和一个管理接收器应用程序.它们都连接到相同的Firebase数据库.当客户进行预订时,我可以在我的管理应用程序中查看此内容.但是,如何在客户应用程序中进行预订后,如何在管理应用程序中收到通知呢?

I have 2 apps a customer booking app and an admin receiver app. they are both connected to the same Firebase database. When a customer makes a booking I can view this in my admin app. But how can I make it so that I receive a notification in the admin app once a booking is made in the customer app?

我已经找到了此代码,但是即使管理应用未打开,我如何实现它以显示通知呢?

I have found this code but how could I implement it so that it shows the notification even if the admin app not open?

 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树看起来像这样

Edit

My Firebase tree looks like this

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

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

The bookings node is a constant and is always there, the user id appears once a booking is made. Could I have some sort of service that runs even when app closed that listens for the "UserID" node to update? then fire the notification method above? I've never worked with notification and services.

推荐答案

管理应用程序的代码

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

这就是您如何在清单中注册它

And thats how you register it in your manifest

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

通知消息:

FCM代表客户端应用程序自动将消息显示给最终用户设备.通知消息具有一组预定义的用户可见键和一个可选的自定义键值对数据有效载荷.

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.

数据消息:

客户端应用负责处理数据消息.数据消息仅具有自定义键值对.

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

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆