应用程式被杀死时未收到android fcm通知

时间:2019-03-19 02:21:35

标签: android firebase firebase-cloud-messaging

我关注了official sample,并扩展了FirebaseMessagingService

<service android:name=".service.MyFirebaseMessagingService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>

和我的推送json

{
"message":{
    "token":"my fcm token",
    "android":{
        "priority": "high",
        "data":{
            "key1":"123","key2":"456"
        }
    }
}

}

我从

接收了fcm

@Override public void onMessageReceived(RemoteMessage remoteMessage) {}

直到我从最近的应用程序中杀死了我的应用程序。

我的android操作系统是Android 8.1(API级别27)

我已经在android 7.1(API级别25)上进行了测试,它可以接收消息,甚至应用程序被杀死。

3 个答案:

答案 0 :(得分:0)

请参阅此部分:

   @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    System.out.println("Remote Data" + remoteMessage);

    //Check if msg contains Data'
    if (remoteMessage.getData().size() > 0) {
        System.out.println("Message Data : " + remoteMessage.getData());
        sendNotification(remoteMessage.getData().get("body"));
    }

    //Check if message contains notification
    if (remoteMessage.getNotification() != null) {

        System.out.println("Message Body " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    }
}

参考链接:

Check Here

答案 1 :(得分:0)

oreo中的@youteng通知必须在通知构建器对象内需要通道ID。因此,请检查您的代码是否添加了频道ID。您可以查看下面的代码。也请看here

// Sets an ID for the notification, so it can be updated.
int notifyID = 1; 
String CHANNEL_ID = "my_channel";// channel id 
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
            .setContentTitle("New Message")
            .setContentText("You've received new messages.")
            .setSmallIcon(R.drawable.ic_notify_status)
            .setChannelId(CHANNEL_ID)
            .build();

答案 2 :(得分:0)

实施FCM时有很多细节。使用工具检查发送方和接收方。还有背景/前景部分。

如果您的消息格式不正确,则当应用程序在后台运行时,它不会触发onReceive()。 correct是指它仅包含data{...},不包含notification{...}

在深入研究服务器端的代码之前,您是否通过从Firebase Console的网络工具或FCM API发送消息来测试客户端?

  

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

我不知道您在服务器端使用的是哪种语言,但是原始消息应该看起来像这样(来自official

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message"
      }
   }
}