如何处理点击推送通知(Firebase 云消息传递)?

发布于 2025-01-20 15:23:14 字数 1339 浏览 0 评论 0原文

Firebase Cloud Messaging发送给我的应用程序。当应用关闭时,它们会出现在系统托盘中。

任务是打开应用程序并在用户单击托盘中的推送通知时执行某些操作(例如打开另一个片段)。

问题是,当用户单击推送通知时,我不知道如何覆盖默认行为。是否有某种回调,广播接收器等?

我的firbasemessagingservice

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

Firebase Cloud Messaging sends pushes to my app. 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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2025-01-27 15:23:14

首先,重要的是要了解应用程序以两种不同的方式处理推送通知,具体取决于应用程序是否位于 前景或背景

当您的应用在后台运行时发送通知消息。
在这种情况下,通知将传递到设备的系统托盘。
默认情况下,用户点击通知会打开应用程序启动器。

在接收时包含通知和数据负载的消息
在这种情况下,通知将发送到
设备的系统托盘,数据有效负载在 extras 中传递
您的启动器 Activity 的意图。

输入图像描述这里

应用程序在后台时的解决方案

在这种情况下,当用户单击系统托盘中的推送通知时,默认情况下会打开该应用程序。 并且您将从intent.extras获取所有需要的数据。因此,基本上在您的主要活动 onCreate 中,您可以简单地从意图中获取额外内容,并根据数据执行一些操作,例如打开一些屏幕。

重要的是,在这种情况下,不会调用 FirebaseMessagingService 中的 onMessageReceived 方法。您可能会在互联网上找到有关它的误导性信息,但我认为过去的行为有所不同。

应用程序位于前台时的解决方案

默认行为是,当应用程序打开时,推送通知不会出现在系统托盘中。但您可以创建自己的 FirebaseMessagingService 并在 onMessageReceived 中接收通知信息。

您可以使用 PendingIntent 显示自定义通知。在 PendingIntent 中,您可以放置​​必须使用所需数据打开的活动。然后,当用户单击通知时,将打开包含数据的活动。

您将再次在 intent.extras 中获得所有需要的数据。因此,基本上在您的主要活动 onCreate 中,您可以简单地从意图中获取额外内容,并根据数据执行一些操作,例如打开一些屏幕。

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.

enter image description here

Solution when app is in the background

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

Important, onMessageReceived method in your FirebaseMessagingService won't be called in this case. You may find misleading info in the internet about it, but I suppose the behaviour was different in the past.

Solution when app is in the foreground

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.

You can show your custom Notification with PendingIntent. In the PendingIntent you can put activity that must be opened with needed data. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文