【问题标题】:cloud function to send FCM from specific app从特定应用程序发送 FCM 的云功能
【发布时间】:2020-05-17 18:11:16
【问题描述】:

我有一个包含两个应用程序(两个目标)的 xCode 项目,我正在尝试从特定目标发送 FCM,但它总是从第一个目标发送!

在 index.js 文件中

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

admin.initializeApp();

exports.sendNotificationForCompany = functions.firestore.document('test/{referenceID}').onCreate(async (snap, context) => {

    var theTitle = "Not"

    let payload = {
          notification: {
              title: theTitle,
              sound: 'default',
          }
      };

    const allTokens = await admin.firestore().collection('users').get();
    const tokens4 = [];
    allTokens.forEach((tokenDoc) => {
      if (tokenDoc.data().instanceIdToken){
        tokens4.push(tokenDoc.data().instanceIdToken)
      }
    });

    admin.messaging().sendToDevice(tokens4, payload);  
});

【问题讨论】:

    标签: firebase google-cloud-functions firebase-cloud-messaging apple-push-notifications


    【解决方案1】:

    您可以根据主题发送通知。例如project-one-topic-unique-user-idproject-two-topic-unique-user-id。所以现在根据主题发送通知

    let message = {
       "topic": "project-two-topic-unique-user-id"
        "notification": {
           title: theTitle,
            sound: 'default',
        }
     }
    }
    
    admin.messaging().send(message)
      .then((response) => {
         // Response is a message ID string.
         console.log('Successfully sent message:', response);
      })
      .catch((error) => {
         console.log('Error sending message:', error);
      });
    

    【讨论】:

    • 根据我对主题的理解,它是针对注册用户而非应用程序的,我已经为第二个应用程序提供了一个单独的用户数据库,因此通知将毫无问题地发送给他们,但通知来自错误的应用程序,如何指定要从第二个应用程序发送的 admin.messaging。
    • 这是我的错误,我忘了替换第二个应用的集合。
    猜你喜欢
    • 2021-03-03
    • 2021-08-09
    • 2020-02-02
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2020-09-26
    相关资源
    最近更新 更多