What is the correct syntax to send Firebase Cloud

2019-07-26 16:25发布

问题:

Simply I want to use the Google Cloud Functions to send notifications to devices that subscribe to a combination of topics.

The documentation says:

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

What I tried to do is:

var topicsConditions = `'${type}' in topics && ('${area}' in topics || '${city}' in topics)`;

// Send a message to devices subscribed to the provided topic.
admin.messaging().sendToCondition(topicsConditions, notificationPayload)
  .then(function(response) {
    // See the MessagingTopicResponse reference documentation for the
    // contents of response.
    console.log("Successfully sent message:", response);
  })
  .catch(function(error) {
    console.log("Error sending message:", error);
  });

But I keep getting this error:

Error sending message: { Error: Invalid argument provided. Raw server response: "Invalid "condition" field: only support 'topics' conditions ". Status code: 400. at FirebaseMessagingError.Error (native) at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16) at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:115:23 at process._tickDomainCallback (internal/process/next_tick.js:135:7) errorInfo: { code: 'messaging/invalid-argument', message: 'Invalid argument provided. Raw server response: "Invalid "condition" field: only support \'topics\' conditions\n". Status code: 400.' }, codePrefix: 'messaging' }

Anyone can direct me to the correct syntax?

Edit: The log output of the topics is:

Topic conditions = 'MyType+' in topics && ('Giza, Egypt ' in topics || 'القاهرة الكبرى' in topics)

回答1:

The characters that may be used in a topic name are limited to:

  • lowercase letters a to z
  • uppercase letters A to Z
  • digits 0 to 9
  • characters - _ . ~ %

Your topic names contain invalid characters +, ,, space, and Arabic.

Further details are in the documentation:

Developers can choose any topic name that matches the regular expression: "[a-zA-Z0-9-_.~%]+"

An example of a valid condition string is:

var topicsConditions = "'Aswan' in topics && ('Giza' in topics || 'Cairo' in topics)";

I successfully used this string in a call to admin.messaging().sendToCondition()