2
votes

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
Add console.log(topicsConditions) to your code to see what the actual value is. Add the output to your post. Maybe type, area, or city are not what you expect.Bob Snyder
For other readers, here is the documentation referenced in the post.Bob Snyder
I did actually, I just removed it from the code snippet because I did not think it is relevant. The output is as I expected, but the thing is, I am not sure if this is what FCM expected. Exact log output below Topic conditions = 'MyType+' in topics && ('Giza, Egypt ' in topics || 'القاهرة الكبرى' in topics)MNassar

1 Answers

4
votes

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()