Search code examples
iospush-notificationios10firebase-cloud-messagingrich-notifications

Using Data in Rich Push notification on iOS with FCM


My question might be bad, but I can't find any answer anywhere, I'm lost...


So I want to display a rich notification with a nice image in iOS 10+.

For this I'm using FCM and an UNNotificationServiceExtension which, if I understood correctly, should get the Data payload, find the image URL and load it before modifying the UNNotificationContent.

The issue I have is that I can't get a hold of this "data".

What I'm sending to FCM is the following :

{
"to": "device_token",
"content_available": true,
"mutable_content": true,
"badge" : 9,
"notification": {
    "title": "You still need the iPhone ?",
    "body": "Give it back if you finished you tests !"
},
"data": {
    "message": "test !",
    "mediaUrl": "http://usr.audioasylum.com/images/2/20352/Cat_and_rose.jpg"
},
"priority": "high"
}

What I get in the phone is :

{
aps =     {
    alert =         {
        body = "Give it back if you finished you tests !";
        title = "You still need the iPhone ?";
    };
    "content-available" = 1;
    "mutable-content" = 1;
};
"gcm.message_id" = "0:1489054783357873%1ee659bb1ee659bb";
mediaUrl = "http://usr.audioasylum.com/images/2/20352/Cat_and_rose.jpg";
message = "Offer!";
}

As I understand it, the mediaURL and message should end up in "aps" and not outside, which is why I can't find them in the extension.

And inside the extension I get : (I split it on coma for more readability)

<UNNotificationRequest: 0x117d3c170;
identifier: FDC13B60-FE5A-40C6-896D-06D422043CCE
content: <UNNotificationContent: 0x117d3a650; title: You still need the iPhone ?
subtitle: (null)
body: Give it back if you finished you tests !
categoryIdentifier: 
launchImageName: 
peopleIdentifiers: ()
threadIdentifier: 
attachments: ()
badge: (null)
sound: (null)
hasDefaultAction: YES
shouldAddToNotificationsList: YES
shouldAlwaysAlertWhileAppIsForeground: NO
shouldLockDevice: NO
shouldPauseMedia: NO
isSnoozeable: NO
fromSnooze: NO
darwinNotificationName: (null)
darwinSnoozedNotificationName: (null)
trigger: <UNPushNotificationTrigger: 0x117d3fe90; contentAvailable: YES
 mutableContent: YES>>

Is the problem how I send information with FCM, or the way I retrieve them? Maybe it's the way I treat them ?

As always, thanks for the help!

Edit : Added code for the receiver (which is just a print in the extension)

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

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

    NSLog(@"------------------- %@ -------------------", request);

    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", request.content];
    self.contentHandler(self.bestAttemptContent);
}

Solution

  • I think you should check self.bestAttemptContent?.userInfo for mediaUrl data.