Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add media attachments to my push notifications in an iOS 10 app?

There are multiple examples how you should set up your project to add rich notifications which use 'media attachments' technology to show images. I've read most of them but something I missed, because my project does not display any rich notifications with this payload (tested with APNS-Tool and Boodle):

{
    "aps":{
        "alert":{
            "title": "Rich test",
            "body": "Dancing Banana"
        },
        "mutable-content": 1
    }
}

The notification is visible, but on 3D Touch, there are no additional infos displayed (like the image or the modified title). The notification extension breakpoints and NSLog messages are also not working.

Here is my demo project: https://github.com/gklka/RichTest

What I've done:

  1. Created a new project
  2. implemented iOS-style authorization:
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if (granted) {
            NSLog(@"Granted");

            [[UIApplication sharedApplication] registerForRemoteNotifications];

        } else {
            NSLog(@"Error registering: %@", error);
        }

    }];
  1. added debug to AppDelegate:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Token: %@", deviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Error: %@", error);
}
  1. Added a new target to the project: Notification Service Extension:

Add new target

  1. Added banana.gif to the project

  2. Added code to add banana attachment into NotificationService.m

    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];

    // Add image attachment
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"banana" withExtension:@"gif"];
    NSError *error;
    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"banana" URL:fileURL options:nil error:&error];
    self.bestAttemptContent.attachments = @[attachment];

What did I miss?

Additional info: The same thing works when I use local notifications instead of remote notifications.

like image 707
gklka Avatar asked Oct 02 '16 22:10

gklka


People also ask

Can you add images to push notifications?

Images for notifications are limited to 1MB in size, and otherwise are restricted by native Android image support.

How do you customize push notifications?

To do so, navigate to the Settings > App Settings page, select the app you'd like to set up custom push notifications for, then scroll down to the push notifications section. Change the option under 'Select push notification server' to 'Custom push notifications'.

How do I add push notifications to my iPhone apps?

Setting up Push Notifications in iOS. Go back to the iOS project you created earlier and select the main target. Under the Signing & Capabilities tab, click on the + Capability button, then select Push Notifications. This will enable your app to receive push notifications from OneSignal.

Can you put links in push notifications?

You can link either items or containers to your message. Items from feeds are also supported. It's important to make sure that your app users have access to the app content that you are linking to. The content of your app needs to be Live in order to send a push notification with an In-App Link draggable.


1 Answers

You're most of the way there. The way you're going to send the attachment is usually as a URL in your payload. However, if you wanted to hard-code the attachment, like your code does, I think it would work, but I think you missed one critical component. I think the service extension does not have access to the main bundle or its resources. If you added a resource to the service extension and tried to load that (using something like [NSBundle bundleForClass:[NotificationService class]]), I suspect it would work.

However, if you send the URL as part of the payload, then you're going to load the image from that URL and not the bundle anyway. In that case, I'm pretty sure you also have to use startAccessingSecurityScopedResource on NSURL (along with stopAccessingSecurityScopedResource).

Hope that helps!

like image 188
Jerry Avatar answered Oct 23 '22 19:10

Jerry