我在用户注册时发送推送通知,它只向注册用户发送通知,我可以将其更改为每个人都使用该应用程序吗?

时间:2017-08-04 14:37:25

标签: android node.js firebase firebase-cloud-messaging

  

下面的代码是我的firebase函数index.js文件

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref("Notifications/{userId}")
    .onWrite(event => {
        var request = event.data.val();
        var payload = {
            data: {
                title: "Welcome to ChitChat Group",
                message: "You may have new messages"
            }
        };

        admin.messaging().sendToDevice(request.token, payload)
            .then(function (response) {
                console.log("Successfully sent message: ", response);
            })
            .catch(function (error) {
                console.log("Error sending message: ", error);
            })    
    });
  

下面的代码包含用户注册时我在哪里创建令牌

String uid = (String) firebaseAuth.getCurrentUser().getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications/"+uid);
                 reference.child("token").setValue(FirebaseInstanceId.getInstance().getToken());

1 个答案:

答案 0 :(得分:1)

您可以让所有用户在registration上订阅一个重要主题,例如newMembers,然后向主题发送通知:

var message = { 
 to: '/topics/newMembers',  
 notification: {
     title: 'title', 
     body: 'body' 
 },
};