【问题标题】:android fcm notification not received when app is killed [duplicate]应用程序被杀死时未收到android fcm通知[重复]
【发布时间】:2019-03-19 02:21:35
【问题描述】:

我关注了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 level 25)上测试过,它可以接收消息,甚至应用被杀了。

【问题讨论】:

  • firebase 消息服务只有在应用程序在后台时才会被调用。一旦应用程序被完全杀死,只有在适用时才会显示前台通知
  • 是的,要注意两种类型的 fcm 消息,前台/后台,它们接收不同部分的数据并以不同方式处理。如果您的应用程序被杀死,它将由系统处理。系统只会读取特定的列。
  • @Wesely 你的意思是使用notification 键吗?我试过了,但它仍然没有收到任何东西。
  • 您使用哪个设备来测试通知?
  • @primo 我有两个设备得到了相同的结果。 1 oppo r17 安卓 8.1。 2 华硕 rog 手机安卓 8.1。

标签: android firebase firebase-cloud-messaging


【解决方案1】:

看这部分:

   @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

【讨论】:

  • 感谢您的回复,但这对我不起作用。这部分我以前做过。
  • @youtenli 你能展示你的代码吗?
  • @youtenli 已检查参考链接?
  • 你的意思是fcm链接吗?我可以在应用程序打开或未终止时收到消息(onPause)。
【解决方案2】:

在 oreo 中删除 @youten 通知必须需要通知构建器对象中的 Channel 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();

【讨论】:

【解决方案3】:

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

如果您的消息格式不正确,当应用处于后台时,它不会触发 onReceive()。 correct 我的意思是它只包含data{...},没有notification{...}

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

发布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"
      }
   }
}

【讨论】:

  • 是的,我通过fcm api 发送,我只尝试了notificationdata 和两者。我可以在应用打开或 onPause 时收到消息。顺便说一句,我可以在 android 7(api 级别 25)上接收消息,甚至应用程序被杀死。
猜你喜欢
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多