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

前端之家收集整理的这篇文章主要介绍了android – 如何处理背景和前景的firebase通知?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在后台前台处理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

原文链接:https://www.f2er.com/android/309050.html

猜你在找的Android相关文章