首页 > 解决方案 > 如何在 iOS 中调试后台通知?

问题描述

我正在尝试在 iOS 上调试后台推送通知,更具体地说,将断点设置到应该处理通知以使用自定义逻辑配置国际化内容的代码中。

我在我的 AppDelegateuserNotificationCenter(_:willPresent:withCompletionHandler:)userNotificationCenter(_:didReceive:withCompletionHandler:).

而且我还创建了一个通知服务扩展并为其添加了一个断点,didReceive(_:withContentHandler:)因为我想改变通知的内容。

当我发送以下有效负载并且我的应用程序位于前台时,中的断点userNotificationCenter(_:willPresent:withCompletionHandler:)被命中并且一切正常:

{
  "aps" : {
    "badge" : 1012,
    "category" : "GENERIC_MESSAGE",
    "mutable-content" : 1
  },
  "translations" : [
    {
      "LanguageCode" : "fr",
      "Title" : "Aimant détaché",
      "Body" : "L'aimant du capteur 002F51AB, associé à l'outil HT-1225 Hitachi Tas, a été détaché."
    },
    {
      "LanguageCode" : "en",
      "Body" : "The magnet of the tag 002F51AB, link to tool HT-1225 Hitachi Tas, was detached.",
      "Title" : "Magnet detached"
    },
    {
      "LanguageCode" : "nl",
      "Body" : "De magneet van de tag 002F51AB, gelinkt aan het gereedschap HT-1225 Hitachi Tas, is losgekoppeld.",
      "Title" : "Magneet losgekoppeld"
    }
  ],
  "messageId" : "90073ebb-ce51-ea11-a94c-000d3a213771"
}

但是,如果应用程序在后台并且我发送完全相同的通知,我不会在userNotificationCenter(_:didReceive:withCompletionHandler:). 当我运行通知服务扩展时,同样的事情,我没有受到打击didReceive(_:withContentHandler:)

我是不是忘记了什么?是因为我的通知负载没有 aps 警报字段吗?

标签: iosswiftpush-notificationapple-push-notifications

解决方案


我想通了:当我在推送通知中添加警报字段时,即使使用虚拟字符串,它也会被转发到我的通知服务。否则不会。


推荐阅读