Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

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:
    NotificationService.h
    NotificationService.m
    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 focussing on the first one, didReceiveRequestWithContentHandler.
  6. The code in this method should look like this:
     

    - (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];

     

    }

    It may seem like a lot is happening here, but it’s straightforward really. Initially, we are exploring the notification’s payload for the dictionary named ‘data’ that contains the key ‘attachment-url’. If this key exists, we download the media at the URL specified in the key’s value. See, easy!

    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 

That’s it! 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 see the results for yourself.

And of course; you could remove the media attachment and just send text, but where’s the fun in that?!

 

 

  • No labels