Search code examples
typescriptflutterfirebase-cloud-messaginggoogle-cloud-messaging

Invalid JSON payload received. Unknown name "tag" at 'message.notification': Cannot find field


Minimum reproducible code:

exports.testNotification = functions.https.onCall(async (data, context) => {
  if (data != null) {
    const message = {
      token: '<fcm_token>',
      notification: {
        title: 'Dummy title',
        body: 'Dummy body',
        tag: 'foo', // This throws runtime error. 
      },
    };

    try {
      const response = await admin
        .messaging()
        .send(message);
    } catch (error) {
      console.log('Error sending message = ', error);
    }
  }

  return null;
});

I am calling this from Flutter:

var callable = FirebaseFunctions.instance.httpsCallable('testNotification');
callable.call({});

Invalid JSON payload received. Unknown name "tag" at 'message.notification': Cannot find field.

errorInfo: { code: 'messaging/invalid-argument', message: Invalid JSON payload received. Unknown name "tag" at 'message.notification': Cannot find field. }, codePrefix: 'messaging'

If I comment out tag field, then I do receive a notification. So, what's wrong in the code, how to use tag then?


Solution

  • When sending messages, the json is composed by a general "notification" section. The "tag" property is not supported here. See this for reference.

    The "tag" property is related to Android, so it should be included in the "notification" property of the Android section. See this for reference.

    This is an example of how a notification that includes platform specific information should be generated. In the Android config I added the "tag":

    {
      "message":{
         "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
         "notification":{
           "title":"Match update",
           "body":"Arsenal goal in added time, score is now 3-0"
         },
         "android":{
           "ttl":"86400s",
           "notification":{
             "click_action":"OPEN_ACTIVITY_1",
             "tag":"foo"
           }
         },
         "apns":{
           "headers":{
             "apns-priority":"5",
           },
           "payload":{
             "aps":{
               "category":"NEW_MESSAGE_CATEGORY"
             }
           }
         },
         "webpush":{
           "headers":{
             "TTL":"86400"
           }
         }
       }
     }