Error Firebase Cloud Functions Typescript Payload

2019-08-16 08:36发布

问题:

Receiving following error message from Firebase Cloud functions log for my Firebase Function. I am trying to create a remote push notification payload with localization which has dynamic values.

Error: Messaging payload contains an invalid value for the "notification.loc-args" property. Values must be strings.

TypeScript code for payload

  var values : String[] = [];
  values.push('Johnny Appleseed');
  const payload = {
    notification: {
      'title-loc-key': 'INVITE_PUSH_TITLE',  
      'loc-key': 'INVITE_PUSH_BODY',
      'loc-args': values,
      'type': 'Invite',
      'fromName': name,
      'userId': uid,
    }
  };

'loc-args' property already contains string array. What is wrong here?

Following sample shows how it suppose to be, based on Apple documentation

Localization Parameter for iOS

"GAME_PLAY_REQUEST_FORMAT" = "%@ and %@ have invited you to play Monopoly";

Payload

{
    "aps" : {
        "alert" : {
            "loc-key" : "GAME_PLAY_REQUEST_FORMAT",
            "loc-args" : [ "Jenna", "Frank"]
        }
    }
}

回答1:

Following payload for FCM solved my problem.

const payload = {
    notification: {
      title: 'You have a new team request!',
      body: `${name} sent team request.!`,
      'title_loc_key': 'TEAM_INVITE_PUSH_TITLE',  
      'body_loc_key': 'TEAM_INVITE_PUSH_BODY',
      'body_loc_args': `["${name}"]`
    },
    data: {
      type: 'teamInvite',
      fromName: name,
      userId: uid
    }
  };