android – 如何处理背景和前景的firebase通知?

android – 如何处理背景和前景的firebase通知?,第1张

概述我想在后台和前台处理firebase通知消息.我将发送一条消息,其中包含来自开发人员的youtube链接,当用户点击通知栏时,它必须指示用户打开链接.有谁知道它是如何完成的? public void onMessageReceived(RemoteMessage remoteMessage) { // [START_EXCLUDE] // There are two types o 我想在后台和前台处理firebase通知消息.我将发送一条消息,其中包含来自开发人员的youtube链接,当用户点击通知栏时,它必须指示用户打开链接.有谁知道它是如何完成的?
public voID onMessageReceived(RemoteMessage remoteMessage) {    // [START_EXCLUDE]    // There are two types of messages data messages and notification messages. Data messages are handled    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type    // Traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app    // is in the foreground. When the app is in the background an automatically generated notification is displayed.    // When the user taps on the notification they are returned to the app. Messages containing both notification    // and data payloads are treated as notification messages. The Firebase console always sends notification    // [END_EXCLUDE]    // Todo(developer): Handle FCM messages here.    // Not getting messages here? See why this may be:    Log.d(TAG,"From: " + remoteMessage.getFrom());    // Check if message contains a data payload.    if (remoteMessage.getData().size() > 0) {        Log.d(TAG,"Message data payload: " + remoteMessage.getData());    }    // Check if message contains a notification payload.    if (remoteMessage.getNotification() != null) {        Log.d(TAG,"Message Notification Body: " + remoteMessage.getNotification().getbody());    }    // Also if you intend on generating your own notifications as a result of a received FCM    // message,here is where that should be initiated. See sendNotification method below.}

接下来要做什么来实现我的目标?提前致谢:)

解决方法 您应该在FCM消息中发送数据有效负载.无论您的应用程序位于前台还是后台,都会以消息方式接收数据有效负载.处理那里的行动.就像通过总是读取数据有效负载来显示通知一样,或者如果您想在应用程序打开或在前台时显示警告对话框.

这是一个示例有效负载:

{  "to": "registration_ID_or_topic","data": {        "message": "This is a Firebase Cloud Messaging topic Message!","youtubeURL": "https://youtu.be/A1SDBIViRtE"   }}

然后在你的onMessageReceived中:

public voID onMessageReceived(RemoteMessage remoteMessage) {   if (remoteMessage.getData().size() > 0) {        Log.d(TAG,"Message data payload: " + remoteMessage.getData());        Map<String,String> receivedMap = remoteMessage.getData();        String youtubeURL = receivedMap.get("youtubeURL");        showNotificationWithURLAction(youtubeURL);   }   .....}

您可以通过Google搜索来轻松实现showNotificationWithURLAction(…)方法.一个样本是here

总结

以上是内存溢出为你收集整理的android – 如何处理背景前景的firebase通知?全部内容,希望文章能够帮你解决android – 如何处理背景和前景的firebase通知?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145302.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存