如何使用Node.js将FCM通知发送到所有android设备

如何使用Node.js将FCM通知发送到所有android设备

问题描述:

我想将通知发送到使用Node.Js代码中的Ionic t开发的Android应用程序.我试过下面的代码并得到Exactly one of topic, token or condition is required.

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

如果要向所有用户发送通知,那么最好的方法是将用户注册到某个主题,例如food,然后每个人都注册到该主题将收到通知.

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.

如果令牌:

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

如果主题:

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

更多信息在这里:

https://firebase.google.com/docs/cloud-消息/管理员/发送消息