Neat
Neat

Reputation: 253

How to use apns-collapse-id with FCM?

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

Upvotes: 9

Views: 4318

Answers (1)

eyalyoli
eyalyoli

Reputation: 2022

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));

Upvotes: 4

Related Questions