Search code examples
iosflutterfirebase-cloud-messagingapple-push-notifications

Unable to get collapseKey in rMessage (RemoteMessage) on iOS while using firebase_messaging package to update existing notification


I am sending the following payload to FCM API

{
  message: {
    token: DEVICE_TOKEN,
    apns: {
      headers: {
        'apns-priority': '5',
        'apns-push-type': 'alert',
        'apns-collapse-id': '113',
      },
      payload: {
        aps: {
          alert: {
            title: 'Brand new title',
            body: 'Brand new text',
          },
          category: 'XYZ_CATEGORY',
        },
      },
    },
  },
}

I have written the following background remote notification handler outside of any class as a top-level function in my Flutter app:

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage rMessage) async {
  await Firebase.initializeApp();
  debugPrint('Handling a background message ${rMessage.toMap()}');
}

Here's the output in the console where the collapseKey is null.

{
  senderId: null,
  category: 'XYZ_CATEGORY',
  collapseKey: null,
  contentAvailable: true,
  data: {},
  from: null,
  messageId: 1706272050712326,
  messageType: null,
  mutableContent: false,
  notification: {
    title: 'Brand new title',
    titleLocArgs: [],
    titleLocKey: null,
    body: 'Brand new text',
    bodyLocArgs: [],
    bodyLocKey: null,
    android: null,
    apple: {
      badge: null,
      subtitle: null,
      subtitleLocArgs: [],
      subtitleLocKey: null,
      imageUrl: null,
      sound: null,
    },
    web: null,
  },
  sentTime: null,
  threadId: null,
  ttl: null,
}

The apns-collapse-id is not mapped to the collapseKey.

My questions:

  1. How do I replace an existing notification if I don't get the collapseKey?
  2. Is there something missing in my request?

Solution

  • The collapseKey is only valid in case of Android. The apns-collapse-id doesn't map to this property in Firebase.

    As per my observation, replacing a notification doesn't have to be done manually. If you send a remote notification with apns-collapse-id as 1, then send the following remote notification with the same id, the iOS handles replacing the older notification with the newer one.