Firebase API在使用时未发送推送通知

10
我正在从Parse迁移到Firebase,但在我们的iOS应用程序中遇到了问题。Firebase API无法将推送通知发送到iOS应用程序。这是我发送到https://fcm.googleapis.com/fcm/send的内容。
{ 
 "to: "<registration token>",
 "priority": "high",
 "data": {
     "customId": "<my custom id>",
     "badge": 1,
     "sound": "cheering.caf",
    "alert": "New data is available"
  }
}

而且服务器返回成功

{
    "multicast_id":6917019914327105115,
    "success":1,
    "failure":0,
    "canonical_ids":0,
    "results":[{"message_id":"0:1468961966677585%f0f84693f9fd7ecd"}]
}

但是推送未被传递。 如果我使用Firebase Dashboard发送推送,即使直接针对令牌进行定位,也会传递推送。
我在另一个Stackoverflow问题中看到另一位开发人员抱怨 无法使用服务器API发送推送通知 我尝试了他们添加“priority”:“high”的解决方案,但它没有解决问题。但它给了我一个线索:他们也在使用dev/sandbox推送证书。
我的怀疑是Dashboard可以使用ios开发证书,但API不能。问题只发生在ios设备上,因为android应用程序通过API获得推送。
有人能够使用API和开发证书发送推送吗?

1
我遇到了相同的问题,但在Android上。 - user2976753
2个回答

22

我得到 Firebase 支持的联系,并找出了问题所在。

我的推送负载缺少通知对象。

{ 
 "to": "<registration token>",
 "priority": "high",
 "notification": {
    "title": "Your Title",
    "text": "Your Text"
  }
 "data": {
     "customId": "<my custom id>",
     "badge": 1,
     "sound": "cheering.caf",
    "alert": "New data is available"
  }
}

我希望这能帮助到其他人


1
请注意,只有当您的应用程序连接到 FCM 时,即应用程序在前台运行时,才能发送数据消息。 - Arthur Thompson
你可能需要将 "content_available":"true" 设置为 iOS 在后台运行。https://firebase.google.com/docs/cloud-messaging/concept-options - Magnus Smith
在 iOS 中发送通知需要文本和标题参数。 - Hardik Shah
2
太棒了!顺便说一下,在通知中您使用了“text”,这很有效,但是Google文档在传统API和新API上都使用了“body”...这只是需要考虑的一些事情(https://firebase.google.com/docs/cloud-messaging/http-server-ref)。 - mkimmet

9
您发送到 Firebase API 的 https://fcm.googleapis.com/fcm/send 的对象应该像这样:
{
  "notification":{
    "title":"Notification title",  //Any value
    "body":"Notification body",  //Any value
    "sound":"default", //If you want notification sound
    "click_action":"<activity_name>",  //Must be present for Android
    "icon":"fcm_push_icon"  //White icon Android resource
  },
  "data":{
    "param1":"value1",  //Any data to be retrieved in the notification callback
    "param2":"value2"
  },
    "to":"/topics/topicExample", //Topic or single device
    "priority":"high", //If not set, notification won't be delivered on completely closed iOS app
    "restricted_package_name":"" //Optional. Set for application filtering
}

请注意,如果您的问题已经得到解决,请不要忘记将其标记为已解决。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接