iOS 10 Rich Push Notifications

The introduction of iOS 10 allows the ability to send push notifications with an image, video, or gif. To enable this feature, there are a few steps that must be done to complete the integration.

1. Create a Notification Service Extension

Within your project, you must create a Service Extension. A Service Extension allows for the modification of a notification to include rich media. To add a notification service extension, click on File -> New -> Target and select Notification Service Extension

2. Setting up Notification Service Extension

Since the Notification Service Extension has it's own bundle id, it must be set up with it's own App ID and provisioning profile. Please verify through the Apple Developer Service. 

3. Add the following code to Notification Service Extension

While you are free to implement your own method within the Notification Service Extension, you will need to make sure that your code is correctly handling the media that is being sent with Leanplum. The key that is associated with the rich media in the Leanplum Payload is: LP_URL. 

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSDictionary *userInfo = request.content.userInfo;
    // LP_URL is the key that is used from Leanplum to
    // send the image URL in the payload.
    // If there is no LP_URL in the payload than
    // the code will still show the push notification.
    if (userInfo == nil || userInfo[@"LP_URL"] == nil) {
    NSString *attachmentMedia = userInfo[@"LP_URL"];
    // If there is an image in the payload, this part
    // will handle the downloading and displaying of the image.
    if (attachmentMedia) {
        NSURL *URL = [NSURL URLWithString:attachmentMedia];
        NSURLSession *LPSession = [NSURLSession sessionWithConfiguration:
         [NSURLSessionConfiguration defaultSessionConfiguration]];
        [[LPSession downloadTaskWithURL:URL completionHandler: ^(NSURL *temporaryLocation, NSURLResponse *response, NSError *error) {
              if (error) {
                  NSLog(@"Leanplum: Error with downloading rich push: %@",
                        [error localizedDescription]);
            NSString *fileType = [self determineType: [response MIMEType]];
            NSString *fileName = [[temporaryLocation.path lastPathComponent] stringByAppendingString:fileType];
            NSString *temporaryDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
            [[NSFileManager defaultManager] moveItemAtPath:temporaryLocation.path toPath:temporaryDirectory error:&error];
              NSError *attachmentError = nil;
              UNNotificationAttachment *attachment =
              [UNNotificationAttachment attachmentWithIdentifier:@""
                                                             URL:[NSURL fileURLWithPath:temporaryDirectory]
              if (attachmentError != NULL) {
                  NSLog(@"Leanplum: Error with the rich push attachment: %@",
                        [attachmentError localizedDescription]);
              self.bestAttemptContent.attachments = @[attachment];
              [[NSFileManager defaultManager] removeItemAtPath:temporaryDirectory error:&error];
          }] resume];
- (NSString*)determineType:(NSString *) fileType {
    // Determines the file type of the attachment to append to NSURL.
    if ([fileType isEqualToString:@"image/jpeg"]){
        return @".jpg";
    if ([fileType isEqualToString:@"image/gif"]) {
        return @".gif";
    if ([fileType isEqualToString:@"image/png"]) {
        return @".png";
    } else {
        return @".tmp";

4. Update didReceiveRemoteNotification

Please make sure to update didReceiveRemoteNotification, so that it does execute the downloading of the rich media. 

5. Create a Rich Push from our Dashboard

After implementing the code above, you are free to create a rich push notification from our dashboard. 


Was this article helpful?
Have more questions? Submit a request