从特定应用程序发送 FCM 的云功能

cloud function to send FCM from specific app

我有一个 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);  
});

您可以根据主题发送通知。例如 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);
  });