Wojtek Dmyszewicz
Wojtek Dmyszewicz

Reputation: 4318

Notification Service app extension mandatory to display media in remote notification?

I'm trying to send an iOS push notification with a media attachment (image url) I've OneSignal SDK 2.2.2 for iOS but it doesn't work at all. In the following article it seems you don't have to implement a Service Extension to display the image inside the notification. (iOS 10).

Do I need to create a Notification Service app extension?

Upvotes: 2

Views: 720

Answers (1)

Danny Yassine
Danny Yassine

Reputation: 681

Yes it is required to implement a Notification Service Extension to handle the push notification payload before submitting it back to the OS to display to the user.

Example: if this is the payload received:

    {
      "aps": {
          "alert": {
            "title": "My title",
            "subtitle": "My title"
            },
           "mutable-content": 1,
           "category": "<your_notification_category>"
          },
        "data": {
          "url": "<img_url>"
        }
    }

Your service must download the image, then bundle the downloaded local URL as an UNNotificationAttachment before sending it off to the OS

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

        // Modify the notification content here...

        if (request.content.userInfo[@"data"] && [request.content.userInfo[@"data"] isKindOfClass:[NSDictionary class]]) {
            NSDictionary *data = request.content.userInfo[@"data"];
            NSLog(@"%@", data);
            NSURL *dataURL = [NSURL URLWithString:data[@"url"]];
            self.task = [[NSURLSession sharedSession] downloadTaskWithURL:dataURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

                if (location) {
                    // Get the location URL and change directory

                    NSString *tempDirectory = NSTemporaryDirectory();
                    NSURL *fileURL = [NSURL fileURLWithPath:tempDirectory];
                    fileURL = [fileURL URLByAppendingPathComponent:[dataURL lastPathComponent]];
                    NSError *error;
                    if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
                        [[NSFileManager defaultManager] removeItemAtPath:[fileURL path] error:&error];
                    }
                    [[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error];

                    // Create UNNotificationAttachment for the notification

                    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:fileURL options:nil error:nil];
                    self.bestAttemptContent.attachments = @[attachment];
                }

                self.contentHandler(self.bestAttemptContent);
            }];
            [self.task resume];

        } else {
            self.contentHandler(self.bestAttemptContent);
        }

    } 
}

Upvotes: 1

Related Questions