代码之家  ›  专栏  ›  技术社区  ›  meagler

当应用程序位于后台时,即使传递了数据负载,也不会收到Firebase通知

  •  1
  • meagler  · 技术社区  · 8 年前

    现在我知道,如果我发送数据消息,即使应用程序在后台,通知也会出现。但我没有收到任何通知,即使在发送数据消息之后。

    我使用Postman进行测试,以下是我的Postman请求正文:

    {
     "to" : (device token),
    
     "data" : {
         "body" : "great match!",
         "title" : "Portugal vs. Denmark",
         "content_available" : true,
         "priority" : "high",
         "sound": "default",
         "time_to_live":"2419200"
      } 
    }
    

    这是我的 onMessageReceived() 功能:

    public void onMessageReceived(RemoteMessage remoteMessage) {
    
        Map<String,String> data=remoteMessage.getData();
        String title=data.get("title");
        String body=data.get("body");
    
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                sendPushNotification(title,body);
    
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }
    

    这就是我 sendPushNotification() 函数如下所示:

    private void sendPushNotification(String title,String message) {
    
        try {
    
            String imageUrl="";
            MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            if(imageUrl.equals("null")){
                mNotificationManager.showSmallNotification(title, message, intent);
                mNotificationManager.playNotificationSound();
            }else{
                mNotificationManager.showBigNotification(title, message, imageUrl, intent);
                mNotificationManager.playNotificationSound();
            }
    
        } catch (Exception e) {
            Log.e(TAG, "Json Exception: " + e.getMessage());
        }
    }
    

    我一直在关注关于堆栈溢出的问题: How to handle notification when app in background in Firebase

    P、 美国:当应用程序位于前台时,通知的接收效果很好。

    4 回复  |  直到 8 年前
        1
  •  2
  •   Antonio    8 年前

    正如你在我的帖子上看到的,对于特定的ID,你应该使用 "registration_ids": ["{device-token}","{device2-token}","{device3-token}"] 键和值,而不是 ”to” . 这是针对主题的

        2
  •  1
  •   karan    8 年前

    您需要定义 click_action 在通知负载中。

    "notification"{
         "click_action":"YOUR_ACTIVITY_NAME"
      }
    

    还可以在清单中编辑活动定义,如下所示

    <activity
                android:name="com.demo.xyzActivity"
                android:windowSoftInputMode="stateHidden">
                <intent-filter>
                    <action android:name=".xyzActivity" /> //This should be one set into payload
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    

    这将在应用程序位于后台时打开您的活动。

        3
  •  0
  •   Chandan Bhandari    8 年前

    您是否将通知负载与数据负载一起使用?由于函数查找通知负载,它将其直接发送到通知托盘,因此忽略了数据负载。 改变

    JSONObject json = new JSONObject(remoteMessage.getData().toString());
    

    Map<String, String> bundleData = remoteMessage.getData();
    

    这将删除try-catch,并让您知道导致异常的原因。

        4
  •  0
  •   Jaspreet Kaur    8 年前

    创建BroadcastReceiver

    在清单中声明。xml

        <uses-permission android:name="android.permission.WAKE_LOCK" />
    
        <receiver
                    android:name=".OnBootBroadcastReceiver">
                    <intent-filter>
                        <action android:name="android.intent.action.BOOT_COMPLETED" />
                    </intent-filter>
        </receiver>
    

    创建OnBootBroadcastReceiver。并调用其中的FCM服务。

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class OnBootBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent i = new Intent("com.example.FirebaseMessagingReceiveService");
            i.setClass(context, FirebaseMessagingReceiveService.class);
            context.startService(i);
    
        }
    }