firebase 我可以使用sendTotopic向多个设备发送推送通知吗?如果我想将其发送到所有角色=A的用户,我该怎么做?

ryoqjall  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(134)

如果我只想向角色为A的用户发送通知,我需要使用令牌吗?
下面是我的代码index.js

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


exports.notifyDoctorregisTopic = functions.firestore.document('clinicreport/{reportId}')
    .onCreate(async (snapshot, context) => {
        const data = snapshot.data();

        // Check the conditions
        if (data.uid !== null && data.status === 'waiting') {
            const payload = {
                notification: {
                    title: 'มีClinicสมัครมา',
                    body: 'A new clinic report is available',
                    clickAction: 'FLUTTER_NOTIFICATION_CLICK',
                },
                data: {
                    click_action: 'FLUTTER_NOTIFICATION_CLICK', // for background
                }
            };

            // Send the notification to the Doctorregis topic
            return admin.messaging().sendToTopic('Doctorregis', payload);
        }
        return null;
    });

字符串
我尝试这个代码,它发送通知给所有用户。

rggaifut

rggaifut1#

主题消息将发送给订阅了某个主题的所有用户。
主题消息传递允许您将消息发送到选择了特定主题的多个设备。您可以根据需要编写主题消息,FCM会处理消息的路由并将其可靠地传递到正确的设备。
订阅

await FirebaseMessaging.instance.subscribeToTopic("roleA");

字符串
发送

admin.messaging().sendToTopic("roleA", payload);


详情请参见Topic messaging on Flutter

相关问题