如何使用Node.js向所有Android设备发送FCM通知

时间:2018-03-18 14:18:01

标签: firebase firebase-authentication firebase-cloud-messaging firebase-notifications

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

如何在没有任何条件的情况下向所有用户发送通知?

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

1 个答案:

答案 0 :(得分:3)

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

在上面的代码中,您收到该错误,因为您没有向谁发送通知。

如果令牌:

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

相关问题