我正在开展一个项目,我们在该项目中使用 Firebase 云消息传递进行推送通知。以下 JSON 当前由后端 API 生成并发送到 FCM:
{
"priority": "normal",
"delay_while_idle": true,
"dry_run": false,
"time_to_live": 3600,
"notification": {
"body_loc_key": "MyCustomNotificationId"
},
"data": {
// contains notification data
},
"registration_ids": [
]
}
这个通知应该是无声的,这意味着它应该只在 iOS 应用程序在前台时收到,但是有时在某些设备上,这个通知也会在应用程序在后台时找到它的方式到 iOS 通知托盘,就好像它有参数要显示在那里。
我发现 iOS 设备必须存在 body_loc_key
属性,否则无论应用程序是在前台还是后台,通知都不会到达设备。
问题出现在以下设备上:
- 苹果 iPhone 5,
- 苹果 iPhone 6,
其他人也可能受到影响。
发送到您成功使用的 FCM 的 JSON 是否有不同的配置,其中通知仅在应用程序位于前台时发送到设备?
最佳答案
在同时摆弄 FCM payload 之后,我们发现问题实际上一直是 body_loc_key
属性。
为了使通知保持静音并仍然发送到 Apple 设备,负载必须满足以下条件:
priority
必须设置为normal
,content_available
必须设置为true
,notification
属性必须包含数据,但如果它包含body_loc_key
属性,则必须将其设置为空字符串 -""
。
有效负载示例:
// Example one
{
"priority": "normal",
"delay_while_idle": true,
"dry_run": false,
"time_to_live": 3600,
"content_available": true,
"notification": {
"body_loc_key": ""
},
"data": {
// contains notification data
},
"registration_ids": [
]
}
// Example two
// (note that body_loc_key has been replaced with badge)
{
"priority": "normal",
"delay_while_idle": true,
"dry_run": false,
"time_to_live": 3600,
"content_available": true,
"notification": {
"badge": 10
},
"data": {
// contains notification data
},
"registration_ids": [
]
}
将 body_loc_key
更改为空字符串几乎可以解决问题。最重要的是,我们还发现了以下关于 other 的信息notification
属性的属性:
badge
可能存在并被处理,通知保持沉默,title_loc_key
没有效果,通知保持沉默,body_loc_args
没有效果,通知保持沉默。
所有三个添加都适用于满足先例标准的场景(空 body_loc_key
if/when present 等)。
关于ios - 来自 FCM 的通知有时会显示在 iOS 通知托盘中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41042311/