Change Push content from Userinfo to ios

When a notification is received, I want to change the user's information content before being shown in a mobile notification on iOS.

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

    }
}

      

Example. I want to show the world instead of a greeting.

Is this possible in iOS 10?

+3


source to share


1 answer


You can implement an extension for it. Here is the link: Extending Notifications

You have to implement didReceive (_: withContentHandler :) method and use it to handle incoming notifications.



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)
   }
}

      

+2


source







All Articles