Rich Push Notifications Integration for Appoxee Standalone

With the new iOS 10 you can attach a static or animated (gif) image, or even a video to push notifications. It will be displayed right in the notification when the user force-taps it. Link to apple video for rich messages support - https://developer.apple.com/videos/play/wwdc2016/708/

 

Create Notification Service Extension

  1. In XCode, Go to File -> New -> Target -> Notification Service Extension



  2. Create Notification Service Extension. Name your extension as NotificationService (You may choose a different name which will be referred to as Notification Service Target (name) in the subsequent sections).

     

  3. Click Activate to finish.

  4. At the end of this step, three new files will be created under NotificationService directory named:
    1. NotificationService.h
    2. NotificationService.m
    3. info.plist

  5. Open this up and you’ll see a couple of delegate methods – one for receiving a notification request and one for handling the expiration of the service extension. We’ll be focusing on the first one, didReceiveRequestWithContentHandler.

  6. The code in this method should look like this:
     

    NotificationService.m
    - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
         self.contentHandler = contentHandler;
         self.bestAttemptContent = [request.content mutableCopy];
         
         NSString *urlString = request.content.userInfo[@"ios_apx_media"];
         NSString *mediaType = @"image";
         
         [self loadAttachmentForUrlString:urlString
                                 withType:mediaType
                        completionHandler:^(UNNotificationAttachment *attachment) {
                            if (attachment) {
                                self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment];
                            }
                            self.contentHandler(self.bestAttemptContent);
                        }];
         
         // Modify the notification content here...
         //self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
     }
     
    - (void)serviceExtensionTimeWillExpire {
         // Called just before the extension will be terminated by the system.
         // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
         self.contentHandler(self.bestAttemptContent);
     }
     
    
     - (NSString *)fileExtensionForMediaType:(NSString *)type {
         NSString *ext = type;
         
         if ([type isEqualToString:@"image"]) {
             ext = @"jpg";
         }
         return [@"." stringByAppendingString:ext];
     }
     
    
     - (void)loadAttachmentForUrlString:(NSString *)urlString withType:(NSString *)type completionHandler:(void(^)(UNNotificationAttachment *))completionHandler  {
         
         __block UNNotificationAttachment *attachment = nil;
         NSURL *attachmentURL = [NSURL URLWithString:urlString];
         NSString *fileExt = [self fileExtensionForMediaType:type];
         
         NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
         [[session downloadTaskWithURL:attachmentURL
                     completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
                         if (error != nil) {
                             NSLog(@"%@", error.localizedDescription);
                         } else {
                             NSFileManager *fileManager = [NSFileManager defaultManager];
                             NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExt]];
                             [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
                             
                             NSError *attachmentError = nil;
                             attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
                             if (attachmentError) {
                                 NSLog(@"%@", attachmentError.localizedDescription);
                             }
                         }
                         completionHandler(attachment);
                     }] resume];


    Note: The push payload has a field "ios_apx_media" which contains the url of the media and the URLSession downloads the media to temporary storage and appends a .tmp file extension, which we need to remove so that the application can infer the file type and display it. So, to do this we need to move the file to the application’s local File Manager and that’s it – our media is ready to be attached to the notification content, and served to the user.

    This is all the code needed in your Xcode project to display rich notifications in your application.

    Now, you just need to make a few adjustments to the server that triggers Push Notifications to your application using our API.

  7. To send the rich pushes we can use Appoxee front end and pass the link as a key to the notification.



  8. Once notification is sent we will receive it like the example below 

As shown in the figure above, you can now send images, videos, audio, and GIFs in your iOS 10 Push Notifications! Simply change the URL to point to another media file and the extension should then be able to handle other media types. 

And of course, you could remove the media attachment and just send text as was the case before this update.