Firebase Cloud Messaging not sending aps payload in correct format for iOS Notification Content & Service Extensions
Asked Answered
Q

4

9

I'm trying to implement notifications using Firebase. The notification is received correctly when the app is in the background or foreground. So, the basic mechanics are working.

Now I've added Content Extensions and Service Extensions to the app. The Content Extension works when I use a local notification, but the Firebase message payload seems incorrect as far as the optional fields are considered. Here is a link to an image of my console:

Firebase Console Image with mutable-content & category options

And here is the Firebase remote notification payload that comes across (with some of the long Google numbers edited for anonymity:

{
    aps = 
    {
        alert = 
        {
        body = "Eureka! 11";
        title = "Patient is not doing well";
        };
    };
    category = provider-body-panel;
    gcm.message_id = 0:149073;
    gcm.n.e = 1;
    google.c.a.c_id = 2825604;
    google.c.a.e = 1;
    google.c.a.ts = 149073;
    google.c.a.udt = 0;
    mutable-content = 1;
}

It appears that the "category" and "mutable-content" are not in the correct place. They should be in the aps payload.

How can I get those options to be in the payload so that my app can correctly parse it and connect it with the Content and Service Extensions?

Quickstep answered 29/3, 2017 at 16:52 Comment(0)
A
13

To start off, I'm going to mention that there are two types of message payloads for FCM. notification and data. See the documentation here

When sending notifications through the Firebase Notifications Console, it will be treated as a notification payload. However, if you add in Custom Data, it will add it in the payload as a custom key-value pair.

For example, in your post, the FCM payload should look something like this:

{
    "notification": {
        "body" : "Eureka!",
        "title": "Patient is not doing well"
    },

    "data": {
        "category": "provider-body-panel",
        "mutable-content" : true,
        "click_action" : "provider-body-panel"
    }
}

What's wrong?

  • click_action should be inside notification.
  • mutable-content should be mutable_content (notice the underscore) and should be on the same level as notification.
  • (this one I might've misunderstood, but) There is no category parameter for FCM, click_action already corresponds to it.

See the docs for the parameters here.

It it is currently not possible to set the value for click_action and mutable_content when using the Firebase Notifications Console. You'll have to build the payload yourself, something like this:

{
    "to": "<REGISTRATION_TOKEN_HERE>",
    "mutable_content" : true,
    "notification": {
        "body" : "Eureka!",
        "title": "Patient is not doing well",
        "click_action" : "provider-body-panel"
    }
}

Then send it from your own App Server. You could also do this by using Postman or cURL

Amathist answered 30/3, 2017 at 3:42 Comment(1)
Thanks! This goes against documentation I found on Google's own pages about using the aps object as a sibling to notification and category inside that. Classic Google of course... Yours is the one that worked. Much appreciated.Fiscal
O
4

"mutable-content should be "mutable_content" (keyword for firebase server to send as mutable-content for IOS) as you mentioned in your post, I think you left out in edit.

Below is an example with also the corrected format for the data section in the json sent to the FCM server. So update would be:

{ 
  "to" : "YOUR firebase messaging registration id here",
  "mutable_content":true,
  "notification": {
    "title": "Its about time",
     "body": "To go online Amigo",
     "click_action": "NotificationCategoryIdentifier ForYourNotificationActions"
  },
  "data":{
    "customKey":"custom data you want to appear in the message payload"
    "media-attachment":"mycustom image url",
    "catalogID":"mycustom catalog for my custom app"
  }
}
Osteomalacia answered 16/6, 2017 at 22:23 Comment(0)
U
1

Update Firebase Admin SDK and use sendMulticast(payload) method

var admin = require("firebase-admin")

admin.initializeApp({
    credential: admin.credential.applicationDefault(),
});

// Create a list containing up to 500 registration tokens.
// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
    'YOUR_REGISTRATION_TOKEN_1',
    // …
    'YOUR_REGISTRATION_TOKEN_N',
];

// See documentation on defining a message payload.
var message = {
    notification: {
        title: '$FooCorp up 1.43% on the day',
        body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
    },
    tokens: registrationTokens,
    apns: {
        payload: {
            aps: {
                'mutable-content': true,      // use single quote  
                'category': 'INVITE_CATEGORY' // use single quote   
            }
        },
    },
};

// Send a message to the device corresponding to the provided
// registration tokens.
admin.messaging().sendMulticast(message)
  .then((response) => {
    if (response.failureCount > 0) {
      const failedTokens = [];
      response.responses.forEach((resp, idx) => {
        if (!resp.success) {
          failedTokens.push(registrationTokens[idx]);
        }
      });
      console.log('List of tokens that caused failures: ' + failedTokens);
    }
  });

Ref: https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices

Unijugate answered 12/3, 2021 at 9:6 Comment(0)
D
0

This worked for me with Cloud functions with Node.js

const payload = {
    notification: {
        title: name,
        body: messageText,
        badge: "1",
        mutable_content: "true"
    },
    data: {
        type: "MESSAGE",
        fromUserId: name,
        attachmentUrl: imageUrl
    }};
Dustproof answered 14/4, 2021 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.