61277

How to send FCM notification to all android devices using Node.js

<h3>Question</h3>

I want to send the notification to my Android app developed using Ionic t from Node.Js code. I have tried following code and getting Exactly one of topic, token or condition is required.

How can I send notification all my users without any condition?

var serviceAccount = require("/path/to/config.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://myApp.firebaseio.com" }); var message = { notification: { title: '$GOOG up 1.43% on the day', body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.' } }; admin.messaging().send(message).then(res=>{ console.log("Success",res) }).catch(err=>{ console.log("Error:",err) })
<h3>Answer1:</h3>

If you want to send a notification to all users, then the best thing is to register the users to a certain topic, example food then everyone registered to that topic will receive a notification.

In your code above, you are getting that error because you did not provide to whom you want to send the notification.

If token:

var registrationToken = 'YOUR_REGISTRATION_TOKEN'; <-- token of user var message = { notification: { title: '$GOOG up 1.43% on the day', body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.' } token: registrationToken };

If topic:

var topic = 'food'; var message = { notification: { title: '$GOOG up 1.43% on the day', body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.' } topic: topic };

more info here:

https://firebase.google.com/docs/cloud-messaging/admin/send-messages

来源:https://stackoverflow.com/questions/49348885/how-to-send-fcm-notification-to-all-android-devices-using-node-js

Recommend

  • Convert a key-value array with duplicate keys into array of object with unique key, and value array
  • MVC 3 Unobtrusive validation of a list
  • Using MVC 5's identity, can't get user name right after logging in
  • duplicate symbols for architecture x86_64 While adding pods from CocoaPods
  • Trying to create and loop through matrix of unbalanced data in R
  • Google App Engine - increasing in Datastore operations
  • How to resize bitmap decoded from URL?
  • Destructor for a linked List
  • send arraylist as parameter in volley request
  • Get Table Cell Values Google Docs
  • JQuery Validation for Duplicates in Form Array
  • WPF: Run Validator on two comboboxes as the same time
  • C99′s Fixed-Width Integer Types
  • MySQL calculation of cumulative sum with a reset condition
  • Select running balance from table credit debit columns
  • How to add a CAGradientLayer to a UIView when using programmatic constraints
  • How to get delivery report for a specific contact number while sending sms to multiple contacts in a
  • combining pandas dataframes of different sampling rates
  • Rebuilding again and again to view the imported QML file changes
  • Calling UDF on Dataframe with Serialization Issue
  • React/Redux. Access state from other component
  • Jekyll - How do I create pages in the root directory?
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • UIScrollView does not restore properly
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Annotate objects in a queryset with next and previous object ids
  • Problems using phonegap / cordova file plugin part 2 - synchronicity
  • Ruby regex for matching simpliest Ruby's regexes
  • How to include associated objects using gon in Rails/jQuery
  • Unable to create Access token grant type in wso2 API manager store to test API
  • Grails - How to implement a foreign key relationship not using an id column?
  • How to call different template for different category archive page in woocommerce
  • Running R's aov() mixed effects model from Python using rpy2