【问题标题】:Notification not receive when Application terminated in android应用程序在android中终止时未收到通知
【发布时间】:2020-02-06 07:33:56
【问题描述】:

我使用 Firebase 推送通知到设备令牌。当应用程序打开或在前台时,我可以很好地收到通知。但是当应用程序在当前任务中被杀死或清除应用程序时,我无法收到通知发送。

我在第一次工作时就尝试过 onMessageReceived。但现在它在杀死应用程序时不起作用。

  • 代码接收通知:

    class MyFirebaseMessagingService : FirebaseMessagingService() {
          override fun onMessageReceived(p0: RemoteMessage) {
              val data = p0!!.data
              Log.e("AAAAAAAAAAAAA: ","data 
              111111111111111111111111111111111111111111111111:" + data["key1"])
         }
    }
    
  • 发布发送通知数据: 发送至:https://fcm.googleapis.com/fcm/send

  • 数据:

    {
    "to" : "token key",
    "data": {
           "key1" : "value1",
           "key2" : "value2",
           "other_key" : true
         }
    }
    

结果,应用正在打开,我可以很好地接收数据,但是当我被杀死时,我无法接收数据。

【问题讨论】:

标签: android firebase kotlin firebase-cloud-messaging


【解决方案1】:

如果您将数据发送到 https://fcm.googleapis.com/fcm/send 如您所见,您使用的是旧版 http 协议here

这在文档中不是很清楚。

在此协议中,当应用处于后台或关闭时接收数据消息,您应该使用此有效负载:

{
"to" : "token key",
"data": {
       "key1" : "value1",
       "key2" : "value2",
       "other_key" : true
     },
"priority" : 10,
"time_to_live" : 60
}

首先以最高优先级 (10) 进行测试,然后根据您的需要降级。还可以根据需要以秒为单位调整 time_to_live。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多