在ios中修改Userinfo中的推送通知内容

时间:2017-05-26 05:42:20

标签: ios objective-c push-notification unusernotificationcenter

收到通知后,我想在iOS中的移动通知中显示之前更改用户信息内容。

"aps": {
    "alert": {
        "body": "hello",
        "sound": "Default"
        "badge": "1"

    }
}

示例我想显示 world 而不是 hello

这可以在iOS 10中使用吗?

1 个答案:

答案 0 :(得分:2)

您可以为其实施通知扩展。这是链接:Notification Extension

您必须实现didReceive(_:withContentHandler :)方法并使用它来处理传入通知。

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  self.contentHandler = contentHandler
  bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

  if let bestAttemptContent = bestAttemptContent {
    // Modify the notification content here
    // Convert received string
    let data = bestAttemptContent.body.data(using: .utf8)!
    // Apply encoded string
    bestAttemptContent.body = String(data: data, encoding: .utf16)

    contentHandler(bestAttemptContent)
   }
}