【问题标题】:Send Notification to device tokens向设备令牌发送通知
【发布时间】:2021-05-25 11:59:05
【问题描述】:

我正在将 Cloud Functions 与 Cloud Messaging 结合使用,并且我想向所有具有特定 userRole 的设备发送通知(请参阅 userRoleList)。 不幸的是,我不知道该怎么做。 例如,我只想将 userRole "Aktive" 的 deviceTokens 推送到 deviceTokens。

到目前为止,这是我的 Cloud Functions 代码:

exports.sendNotificationAusschuss = functions.firestore.document('news/{newsId}').onCreate(async snapshot => {
    const news = snapshot.data();
    console.log('Message received');

    //var deviceTokens = ??
    

    const payload = {
        notification:{
            title: 'Message received',
            body: `${news.newsText}`,
            sound: "default"
        }
    };

        return admin.messaging().sendToDevice(deviceTokens, payload);

    

    
});

非常感谢

【问题讨论】:

    标签: node.js google-cloud-firestore google-cloud-functions firebase-cloud-messaging firebase-admin


    【解决方案1】:

    您可以查询 Firestore 以检索具有给定角色的用户。

    const snap = await admin.firestore().collection('users')
      .where('userRoleList', 'array-contains', 'Aktive')
      .get();
    const tokens = [];
    snap.docs.forEach((doc) => {
      tokens.push(doc.data().deviceToken);
    });
    

    然后将tokens分成500个批次,然后:

    await admin.messaging().sendMulticast({
      tokens,
    });
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多