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

San*_*gde 3 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)
})
Run Code Online (Sandbox Code Playgroud)

Pet*_*dad 6

如果要向所有用户发送通知,则最好的办法是将用户注册到某个主题,例如,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
};
Run Code Online (Sandbox Code Playgroud)

如果主题:

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
};
Run Code Online (Sandbox Code Playgroud)

更多信息在这里:

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

  • 无法像在Firebase通知控制台中发送所有用户那样指定软件包? (2认同)