How to use apns-collapse-id with FCM?
Asked Answered
P

1

9

I know this question have been asked many times but none of the answers work for me. Some of the questions dont even have an answer.

I am try to bundle or group similar notifications on ios. I am using FCM Cloud Functions to trigger notifications.

Following are the methods i tried by

const payload = {
        notification: {
          title: "Your medical history is updated" + Math.random(),
          tag: "MedicalHistory",
        },
        data: {
          click_action: "FLUTTER_NOTIFICATION_CLICK",
          sound: "default",
          status: "done",
        },
      };

      const patchedPayload = Object.assign({}, payload, {
        apns: {
          headers: {
            "apns-collapse-id": "MedicalHistory",
          },
        },
      });

      const options = {
        priority: "high",
        collapseKey: "MedicalHistory",
      };


await admin
          .messaging()
          .sendToDevice("MY_TOKEN", patchedPayload, options);

The above code does not work

const payload = {
        notification: {
          title: "Your medical history is updated" + Math.random(),
          tag: "MedicalHistory",
        },
        data: {
          click_action: "FLUTTER_NOTIFICATION_CLICK",
          sound: "default",
          status: "done",
        },
        apns: {
          headers: {
            "apns-collapse-id": "MedicalHistory",
          },
        },
      };



const options = {
            priority: "high",
            collapseKey: "MedicalHistory",
          };

 await admin
              .messaging()
              .sendToDevice("MY_TOKEN", payload, options);

This does not work as well.

I dont understand where to put the apns-collapse-id. The cloud functions sample also does not show this. Neither i could find doc with code on this

Provincial answered 9/4, 2020 at 10:51 Comment(2)
Can you just try changing your options parameter collapseKey with collapse_key? I looked at your 2nd payload looks like you have put apns-collapse-id. at the correct place.Cater
perhaps sendToDevice function gives a promise so put .then and .catch and try to look for the errors. imclude the logs in your questionPiwowar
T
4

I recommend using the latest send function from the firebase-admin lib, usage described here.

It seems to work fine and somewhat easier to apply.

An example of a message with android/ios specific headers:

// common data for both platforms
const notification = {
 title: 'You liked!',
 body: 'You really liked something...',
}
const fcmToken = '...'

// android specific headers
const android = {
  collapseKey: '0'
}

// ios specific headers
const apns = {
  headers: {
    "apns-collapse-id": '0'
  }
}

// final message
const message = {
 token: fcmToken,
 notification: notification,
 android: android,
 apns: apns,
}

// send message
admin.messaging().send(message).catch(err => console.error(err));
Tidy answered 8/2, 2021 at 10:53 Comment(1)
for android, collapseKey is now collapse_key. so it will be collapse_key:'YOUR_KEY'Gamin

© 2022 - 2024 — McMap. All rights reserved.