ios - 来自 FCM 的通知有时会显示在 iOS 通知托盘中

标签 ios firebase-cloud-messaging

我正在开展一个项目,我们在该项目中使用 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/

相关文章:

ios - 如何防止单元格在另一个单元格中镜像按钮按下操作?

c# - UIViewController 子类(模仿 UITableViewController)未发布

ios - AVCaptureSession 运行时崩溃。 fatal error : unexpectedly found nil while unwrapping an Optional value (lldb)

flutter - Firebase_messaging onResume 和 onLaunch 回调不会在 Flutter 中调用

ios - didReceiveRemoteNotification 未通过 FCM 接收通知

iphone - 应用程序在 App Store 中可用后,是否可以更改开发者/卖家帐户?

ios - tableView 上的 UIPanGestureRecognizer 不会滚动表格项目

android - 如何触发应用程序向我的服务器发送数据?

javascript - 通过 Firebase 功能发送时 FCM 出现高延迟

react-native - React Native Android后台处理程序中重复的fcm推送通知