Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Notification service extension with UNNotification in iOS10

Apple introduce new extension names "UNNotificationServiceExtension", but how to launch it from push notification ?

I read that service extension provide end to end encryption for payload.

Which key is required to set payload of push notification ?

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

like image 396
technerd Avatar asked Oct 14 '16 10:10

technerd


People also ask

What is remote notification extension on iPhone?

A UNNotificationServiceExtension object provides the entry point for a notification service app extension. This object lets you customize the content of a remote notification before the system delivers it to the user.

Do I require distribution provisioning profile for notification service extension for iOS?

Yes you have to use separate provisioning profile for Notification Service Extension and Notification Content Extension.


1 Answers

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 .

like image 138
mickeymoon Avatar answered Sep 20 '22 07:09

mickeymoon