How to use Notification service extension with UNNotification in iOS10

Let me take it step by step.

UNNotificationServiceExtension - What it is?

UNNotificationServiceExtension is an App Extenstion target that you bundle along with your app aiming to modify the push notifications as and when they are delivered to the device before rendering it to the user. You can change the title, subtitle, body and additionally add attachments to the push notification by either downloading it or using one bundled in the app.

How to create

Go to File -> New -> Target -> Notification Service Extension and fill in the details

Which key is required to set payload of push notification?

You need to set the mutable-content flag to 1 to trigger the service extension. Also, if the content-available is set to 1, the service extension will not work. So either don't set it or set it to 0. (Edit: This is not applicable. You can set or unset content-available flag)

How to identify payload and how to launch service extension from push notification?

Build the extension and then build and run your app. Send a push notification with the mutable-content set to 1.

Code

UNNotificationService exposes two functions:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
               withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler;

- (void)serviceExtensionTimeWillExpire;

The first function is triggered when the push notification is received on the device and before it is presented to the user. You code inside the function has the opportunity to modify the content of the push notification inside this function.

You do this by modifying the bestAttemptContent property of your extension which is an instance of UNNotificationContent and has properties: title, subtitle, body, attachments etc.

The original payload of the remote notification is delivered via request.content property of function parameter request.

Finally you dispatch your bestAttemptContent using the contentHandler:

self.contentHandler(self.bestAttemptContent); 

You have limited time to do your stuff in the first method. In case that time expires your second method is called with the best attempt your code had made thus far.

Sample Code

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
               withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];

    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    self.contentHandler(self.bestAttemptContent);
}

The above code appends [modified] to the original title in the PN payload.

Sample Payload

{
    "aps": {
        "alert": {
            "title": "Hello",
            "body": "body.."
        },
        "mutable-content":1,
        "sound": "default",
        "badge": 1,

    },
  "attachment-url": ""
}

Please note that the attachment-url key is a custom key for your own concerns and not recognised by iOS .


Notification service extension very useful in download content from notification data and display in notification like image and other content. you can also use notification service extension for execute some code when application is in background mode or in-active(killed).

Here is step by step guidelines of how to use notification service extension. And also you got the demo from from github.