Search code examples
javascriptgoogle-cloud-functionsfirebase-cloud-messagingfirebase-admin

How do I add a channelId to my notification?


I've written a cloud function to send a cloud message to a specific device.

export const onGroupInvitationCreate = functions.region('europe-west1').firestore.document('users/{userId}/userGroupInvitations/{invitationId}').onCreate(async (handler, context) => {

    const invitation = GroupInvitationModel.fromFirestoreDocumentData(handler);

    const documentList = await userCollection.doc(invitation.receiverId).collection('userDeviceData')
    .listDocuments();

    const futureDocuments = [];

    for(const documentReference of documentList)
        futureDocuments.push(documentReference.get());

    const deviceDatas = (await Promise.all(futureDocuments)).map(deviceData => DeviceDataModel.fromFirestoreDocumentData(deviceData));

    const futureNotifications = [];

    for(const deviceData of deviceDatas)
    {
        const translator = deviceData.language !== 'nl' ? languages.en : languages.nl;

        const payload: admin.messaging.MessagingPayload = {
            // notification: {
            //     title: `${translator['invitationTitle' as keyof typeof translator]}`,
            //     body: `${invitation.senderDisplayName} ${translator['invitationBody' as keyof typeof translator]} ${invitation.groupDisplayName}`,
            //     clickAction: 'FLUTTER_NOTIFICATION_CLICK',
            // },            
            data: {
                title: `${translator['invitationTitle' as keyof typeof translator]}`,
                body: `${invitation.senderDisplayName} ${translator['invitationBody' as keyof typeof translator]} ${invitation.groupDisplayName}`,
                clickAction: 'FLUTTER_NOTIFICATION_CLICK',
                senderId: invitation.senderId,
                senderDisplayName: invitation.senderDisplayName,
                type: 'invitation',
                sentAt: new Date().toISOString(),
            }
        }

        futureNotifications.push(messaging.sendToDevice(deviceData.cloudMessagingToken, payload));
    }

    await Promise.all(futureNotifications).catch((error) => console.error(`There was an error while notifying receiver ${invitation.receiverId}`, error));

    return Promise.resolve('Status 200');
});

How would I provide a channelId? I am using the

messaging.sendToDevice();

Which takes a registrationToken, a payload and options. And I noticed that none of those have the propery channelId.

The AndroidConfig does have a property channelId, but I couldn't find how to use that when I send the notification.

This is what the sendToDevice takes as arguments

sendToDevice(
  registrationToken: string | string[],
  payload: admin.messaging.MessagingPayload,
  options?: admin.messaging.MessagingOptions
): Promise<admin.messaging.MessagingDevicesResponse>;

Solution

  • You need to pass the android_channel_id property in the notification section of MessagingPayload. See table 2b in https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support for a complete list of all the Android-related options accepted by the legacy FCM APIs.

    If you wish to use AndroidConfig, you must first migrate to the new FCM API (i.e. the send() method).