3

When receiving a notification, I want to change the user info content before showing in mobile notification in iOS.

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

    }
}

Example I want to show world instead of hello.

Is this possible in iOS 10?

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
SANTOSH
  • 183
  • 1
  • 15
  • Please refer - https://stackoverflow.com/questions/41345889/its-possible-to-change-push-notification-message-before-display-on-device-from –  May 26 '17 at 05:45

1 Answers1

2

You can implement notification extension for it. Here is link : Notification Extension

You must implement the didReceive(_:withContentHandler:) method and use it to process 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)
   }
}
  • objective c if possible – SANTOSH May 26 '17 at 05:48
  • 1
    Please have a look on - https://developer.apple.com/reference/usernotifications/unnotificationserviceextension/1648229-didreceivenotificationrequest?language=objc –  May 26 '17 at 05:49
  • I found this -(void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler; if you don't mind can you help me out on this ...to same how to change in objective c for aps notification . – SANTOSH May 26 '17 at 06:08
  • 1
    https://code.tutsplus.com/tutorials/ios-10-creating-custom-notification-interfaces--cms-27251 will generate for **local-notification** ?? – SANTOSH May 29 '17 at 10:09