Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 491
Neat Avatar asked Apr 09 '20 10:04

Neat


People also ask

What is APNs collapse ID?

The APNs request header key for the collapse identifier is apns-collapse-id and is defined in Table 6-2. For example, a news service that sends the same headline twice in a row could employ the same collapse identifier for both push notification requests.

What is collapse key in FCM?

FCM allows a maximum of four different collapse keys per device to be used by the app server at any given time. In other words, the FCM connection server can simultaneously store four different collapsible send-to-sync messages per device, each with a different collapse key.

Does FCM use APNs?

APNS channel The most common way is sending the message via APNs. If you're using the FCM v1 HTTP API to request a message delivery or if you're using the legacy FCM API to send display messages, FCM will contact APNs to deliver the notification.


Video Answer


1 Answers

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));
like image 133
eyalyoli Avatar answered Oct 16 '22 09:10

eyalyoli