下面的代码是我的火力基础函数索引.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());
您可以让所有用户订阅一个大主题,例如registration
onnewMembers
然后向该主题发送通知:
var message = {
to: '/topics/newMembers',
notification: {
title: 'title',
body: 'body'
},
};