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

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

Below code is my firebase function index.js file

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

below code contains where i crate token when user get registered

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

最佳答案

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

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

关于android - 我在用户注册时发送推送通知,它只向注册用户发送通知,我可以将其更改为每个使用该应用程序的人吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45509551/

相关文章:

android - 如何在android中通过彩信发送vcf文件

javascript - Firebase 日期范围查询不起作用

javascript - 使用 Vue、Vuex 和 Firebase 进行 Twitter 身份验证

android - Phonegap 中的本地存储限制

java - 将值设置为静态变量返回错误

android - 从 History api 接收的步数数据与 google fit 不匹配

javascript - 如何以 jison 形式将附加输入传递给 `parse`?

node.js - shelljs "not found"错误

node.js - NPM 似乎对 Linux Mint 15 没有任何作用

Angular 2 从组件或服务订阅?