简体   繁体   English

如何处理点击推送通知(Firebase Cloud Messaging)?

[英]How to handle click on push notification (Firebase Cloud Messaging)?

Firebase Cloud Messaging sends pushes to my app. Firebase Cloud Messaging 将推送发送到我的应用程序。 And they appear in the system tray when the app is closed.当应用程序关闭时,它们会出现在系统托盘中。

The task is to open the app and perform some action (like open another fragment for example) when user clicks on the push notification in the tray.任务是在用户单击托盘中的推送通知时打开应用程序并执行一些操作(例如打开另一个片段)。

The problem is I don't know how to override the default behaviour when user clicks on the push notification.问题是我不知道如何在用户点击推送通知时覆盖默认行为。 Is there some kind of callback, broadcast receiver etc?是否有某种回调、广播接收器等?

My FirebaseMessagingService我的 FirebaseMessagingService

class FCMHandlerService : FirebaseMessagingService() {

    private val intercomPushClient = IntercomPushClient()

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        logd("onMessageReceived $remoteMessage")
        val notification = remoteMessage.notification ?: return
        logd("Remote message body${notification.body} channel id${notification.channelId} message id${remoteMessage.messageId}")
        
        //you can create here your custom notification when the app receives push being foreground
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        logd("onNewToken $token")
        instance.sendFirebasePushRegistrationToken(token)
    }
}

Service in manifest清单中的服务

        <service
            android:name=".old.push.FCMHandlerService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

First, it's important to understand that application handles push notification in two different ways depending on wether the app is in the foreground or background .首先,重要的是要了解应用程序根据应用程序是在前台还是在后台以两种不同的方式处理推送通知。

Notification messages delivered when your app is in the background.当您的应用程序处于后台时发送的通知消息。 In this case, the notification is delivered to the device's system tray.在这种情况下,通知将传送到设备的系统托盘。 A user tap on a notification opens the app launcher by default.默认情况下,用户点击通知会打开应用程序启动器。

Messages with both notification and data payload, when received in the background.在后台接收时,具有通知和数据负载的消息。 In this case, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.在这种情况下,通知会传送到设备的系统托盘,并且数据负载会在启动器 Activity 的 intent 的附加项中传送。

在此处输入图像描述

Solution when app is in the background App在后台时的解决方法

In that case when user clicks on the push notification in the system tray, the app is opened by default.在这种情况下,当用户单击系统托盘中的推送通知时,应用程序默认打开。 AND you'll get all needed data from the intent.extras .并且您将从intent.extras获得所有需要的数据。 So basically in your main activity onCreate you can simply get extras from intent and perform some action depending on the data, like open some screen.所以基本上在您的主要活动onCreate中,您可以简单地从意图中获取额外内容并根据数据执行一些操作,例如打开一些屏幕。

Important, onMessageReceived method in your FirebaseMessagingService won't be called in this case.重要的是,在这种情况下不会调用FirebaseMessagingService中的onMessageReceived方法。 You may find misleading info in the inte.net about it, but I suppose the behaviour was different in the past.您可能会在 inte.net 中找到有关它的误导性信息,但我认为过去的行为有所不同。

Solution when app is in the foreground App在前台时的解决方法

The default behaviour is that the push notification doesn't appear in the system tray when the app is opened.默认行为是当应用程序打开时,推送通知不会出现在系统托盘中。 But you can create your own FirebaseMessagingService and receive notification info in the mo onMessageReceived .但是您可以创建自己的FirebaseMessagingService并在 mo onMessageReceived中接收通知信息。

You can show your custom Notification with PendingIntent.您可以使用 PendingIntent 显示您的自定义通知。 In the PendingIntent you can put activity that must be opened with needed data.在 PendingIntent 中,您可以放置必须使用所需数据打开的活动。 Then when user click on the notification the activity with data will be opened.然后,当用户单击通知时,将打开带有数据的活动。

Again you'll get all needed data in the intent.extras .同样,您将在intent.extras中获得所有需要的数据。 So basically in your main activity onCreate you can simply get extras from intent and perform some action depending on the data, like open some screen.所以基本上在您的主要活动onCreate中,您可以简单地从意图中获取额外内容并根据数据执行一些操作,例如打开一些屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Firebase Cloud Messaging 安排本地推送通知? - How do I schedule a local push notification with Firebase Cloud Messaging? Firebase 云消息 - 手机收不到推送通知 - Firebase Cloud Messaging - Mobile not receiving push notification 使用 curl 发送数据推送通知 firebase 云消息 - Using curl to send data push notification with firebase cloud messaging Firebase Cloud Messaging 静默推送通知无法正常工作 - Firebase Cloud Messaging Silent Push Notification fail to work 在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递 - Sending FCM Push Notification in Kotlin Client App - Firebase Cloud Messaging 如何使用 Firebase 云消息在 Twilio 上配置新的服务器密钥以进行推送通知 - How to configure new Server Key on Twilio using Firebase cloud messaging for push notification 如何处理Django中的Firebase云消息通知? - How to handle Firebase Cloud Messaging Notifications in Django? 如何处理多个 Firebase 云消息令牌? - How to handle multiple Firebase cloud messaging tokens? 当我单击 FCM(Firebase 云消息传递)通知时,应用程序重新启动 - When I click on a FCM (Firebase Cloud Messaging) notification, the application restarts Firebase 云消息删除通知 - Firebase Cloud Messaging deleting notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM