如何在Firebase中的后台应用中处理通知 [英] How to handle notification when app in background in Firebase

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

问题描述

这是我的清单

 < service android:name =。fcm.PshycoFirebaseMessagingServices> 
< intent-filter>
< action android:name =com.google.firebase.MESSAGING_EVENT/>
< / intent-filter>
< / service>

< service android:name =。fcm.PshycoFirebaseInstanceIDService>
< intent-filter>
< action android:name =com.google.firebase.INSTANCE_ID_EVENT/>
< / intent-filter>
< / service>

当应用程序在后台并且通知到达时,默认通知会自动运行 onMessageReceived



这是我的 onMessageReceived 代码。这将调用如果我的应用程序在前台运行,而不是在后台应用程序。

  // [START receive_message] 
@Override $如何在应用程序在后台运行此代码? b $ b public void onMessageReceived(RemoteMessage remoteMessage){
// TODO(developer):在这里处理FCM消息。
//如果应用程序在前台处理数据和通知消息。
//同样,如果您打算根据收到的FCM
//消息生成您自己的通知,那么这里是应该启动的地方。请参阅下面的sendNotification方法。
data = remoteMessage.getData();
String title = remoteMessage.getNotification()。getTitle();
String message = remoteMessage.getNotification()。getBody();
String imageUrl =(String)data.get(image);
String action =(String)data.get(action);
Log.i(TAG,onMessageReceived:title:+ title);
Log.i(TAG,onMessageReceived:message:+ message);
Log.i(TAG,onMessageReceived:imageUrl:+ imageUrl);
Log.i(TAG,onMessageReceived:action:+ action);

if(imageUrl == null){
sendNotification(title,message,action);
} else {
new BigPictureNotification(this,title,message,imageUrl,action);


// [END receive_message]


解决方案

1。为什么会发生这种情况?



FCM中有两种类型的消息(Firebase云消息传递):


  1. 显示消息:仅当您的应用处于前景时,这些消息才会触发 onMessageReceived()回调>

  2. 数据消息:这些消息触发 onMessageReceived()回调甚至如果您的应用程序位于前景/后台/杀死



  3. Firebase团队尚未开发用户界面 data-messages 到您的设备。





    2。如何?



    为了达到这个目的,你必须执行 POST 到以下网址:


    POST https://fcm.googleapis.com/fcm/send


    以下内容header:


    • 键: Content-Type , 价值: Authorization Value: key =< your-server-key>



    • 正文使用主题

        {
      to:/ topics / my_topic,
      data:{
      my_custom_key:my_custom_value,
      other_key:true
      }

      $ / code>

      或者如果您想将其发送到特定设备:

        {
      data:{
      my_custom_key: my_custom_value,
      other_key:true
      },
      registration_ids:[{device-token},{device2-token},{device3-token} ]
      }




      注意:确保您不添加 JSON密钥通知

      注意:要获取您的服务器密钥,您可以在Firebase控制台中找到它:您的项目 - >设置 - >项目设置 - >云消息传递 - >服务器密钥




      3。如何处理推送通知消息?



      这是您如何处理收到的消息:

        @Override 
      public void onMessageReceived(RemoteMessage remoteMessage){
      Map< String,String> data = remoteMessage.getData();
      String myCustomKey = data.get(my_custom_key);

      //管理资料
      }


      Here is my manifest

          <service android:name=".fcm.PshycoFirebaseMessagingServices">
              <intent-filter>
                  <action android:name="com.google.firebase.MESSAGING_EVENT" />
              </intent-filter>
          </service>
      
          <service android:name=".fcm.PshycoFirebaseInstanceIDService">
              <intent-filter>
                  <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
              </intent-filter>
          </service>
      

      When the app is in background and notification arrives then the default notification comes and doesn't run my code of onMessageReceived.

      Here is my onMessageReceived code. This invokes if my app is running on foreground, not when app in background. How to run this code when the app is in background too?

      // [START receive_message]
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          // TODO(developer): Handle FCM messages here.
          // If the application is in the foreground handle both data and notification messages here.
          // 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.
          data = remoteMessage.getData();
          String title = remoteMessage.getNotification().getTitle();
          String message = remoteMessage.getNotification().getBody();
          String imageUrl = (String) data.get("image");
          String action = (String) data.get("action");
          Log.i(TAG, "onMessageReceived: title : "+title);
          Log.i(TAG, "onMessageReceived: message : "+message);
          Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
          Log.i(TAG, "onMessageReceived: action : "+action);
      
          if (imageUrl == null) {
              sendNotification(title,message,action);
          } else {
              new BigPictureNotification(this,title,message,imageUrl,action);
          }
      }
      // [END receive_message]
      

      解决方案

      1. Why is this happening?

      There are two types of messages in FCM (Firebase Cloud Messaging):

      1. Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground
      2. Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed

      Firebase team have not developed a UI to send data-messages to your devices, yet.

      2. How to?

      To achieve this, you have to perform a POST to the following URL:

      POST https://fcm.googleapis.com/fcm/send

      And the following headers:

      • Key: Content-Type, Value: application/json
      • Key: Authorization, Value: key=<your-server-key>

      Body using topics:

      {
          "to": "/topics/my_topic",
          "data": {
              "my_custom_key" : "my_custom_value",
              "other_key" : true
           }
      }
      

      Or if you want to send it to specific devices:

      {
          "data": {
              "my_custom_key" : "my_custom_value",
              "other_key" : true
           },
          "registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
      }
      


      NOTE: Be sure you're not adding JSON key notification
      NOTE: To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

      3. How to handle the push notification message?

      This is how you handle the received message:

      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) { 
           Map<String, String> data = remoteMessage.getData();
           String myCustomKey = data.get("my_custom_key");
      
           // Manage data
      }
      

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

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