最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send FCM notification to all android devices using Node.js - Stack Overflow

programmeradmin3浏览0评论

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: ""
});

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

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."
});

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)
})
Share Improve this question edited Dec 5, 2019 at 9:19 Peter Haddad 81k26 gold badges145 silver badges147 bronze badges asked Mar 18, 2018 at 14:18 Santosh HegdeSantosh Hegde 3,52014 gold badges39 silver badges55 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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./docs/cloud-messaging/admin/send-messages

发布评论

评论列表(0)

  1. 暂无评论