如何处理后台以及前台的Firebase通知? [英] How to handle firebase notification on background as well as foreground?

查看:266
本文介绍了如何处理后台以及前台的Firebase通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在后台以及在前台处理Firebase通知消息.我将发送一条消息,其中包含来自开发人员的youtube链接,当用户点击通知栏时,它必须指示用户打开该链接.有谁知道它是怎么做的?

I want to handle firebase notification message on background as well as on foreground . I will send a message that will consist a youtube link from developer and when user tap on notification bar it must direct user to open the link. Does anyone knows how it is done?

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

下一步要实现我的目标是什么?在此先感谢:)

what to do next to achieve my goal? Thanks in advance:)

推荐答案

您应该在FCM消息中发送数据有效载荷.无论您的应用程序处于前台还是后台,都可以通过消息方法接收数据有效负载.在那儿处理动作.就像总是通过读取数据有效载荷来显示通知,或者想要在应用打开或在前台运行时显示警报对话框.

You should send data payload in your FCM message. Data payload gets received in on message method irrespective of your app being in foreground or background. Handle the action there. Like show notification by reading the data payload always, or if you want show an alert dialog when your app is open or in the foreground.

以下是有效负载示例:

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

然后在您的onMessageReceived中:

Then in your 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);
   }
   .....
}

您可以通过谷歌搜索来轻松实现showNotificationWithURLAction(...)方法.一个示例是此处

you can easily implement showNotificationWithURLAction(...) method by googling it out. One sample is here

这篇关于如何处理后台以及前台的Firebase通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆