首页 > 解决方案 > 即使发送(消息)成功,通知也没有到达 iOS 设备

问题描述

我正在尝试构建一个 Firebase 云功能来发送通知。我可以使用 FCM 令牌使用控制台直接向 iOS 设备发送通知,并且我的 iOS 设备会显示通知。但是,即使 FCM 令牌相同并且 send(message) 调用成功,设备也没有使用下面的云功能收到通知。我错过了什么吗?

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

admin.initializeApp(functions.config().firebase);

var db = admin.firestore();

exports.requestCreated = functions.firestore
.document('users/{userId}')
.onWrite((change, context) => {
  const createdBy = context.params.userId;
  console.log("Request created by ",createdBy);
  var userRef = db.collection('users').doc(createdBy);
  return userRef.get().then(doc => {
  console.log('Data: ',doc.data());
  console.log('FCM token: ',doc.data().fcmToken);

      // This registration token comes from the client FCM SDKs.
      var registrationToken = doc.data().fcmToken;

      // See documentation on defining a message payload.
      var message = {
        data: {
          score: '850'
        },
        token: registrationToken
      };

      // Send a message to the device corresponding to the provided
      // registration token.
      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);
        });
  });
});

标签: iosnode.jsfirebasenotificationsgoogle-cloud-functions

解决方案


//在消息负载中添加通知

var message = {
        data: {
          score: '850'
        },
        notification: {
          title: "",
          body: ""
        }
        token: registrationToken
      };

您的代码通常在 Android 中运行。在 IOS 中,您需要在消息有效负载中附加通知并使用 sendToDevice()。试试这个。您必须使用此有效负载获取分数。会工作


推荐阅读