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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...