ios - 即使发送(消息)成功,通知也未到达 iOS 设备

标签 ios node.js firebase notifications google-cloud-functions

我正在尝试构建 Firebase 云函数来发送通知。我可以使用 FCM token 通过控制台直接向 iOS 设备发送通知,并且我的 iOS 设备会显示该通知。但是,即使 FCM token 相同并且发送(消息)调用成功,设备也不会使用下面的云功能接收通知。我错过了什么吗?

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);
        });
  });
});

最佳答案

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

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

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

关于ios - 即使发送(消息)成功,通知也未到达 iOS 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53789822/

相关文章:

javascript - 如何正确设置工厂

ios - 获取用户的当前位置/坐标

ios - 多语言 iOS 应用程序中的启动画面

node.js - gruntjs 0.4 registerHelper 已弃用,语法修复?

firebase - 在 Firebase Cloud Functions 中记录 'jsonPayload'

ios - Firebase getDocument(querySnapshot)不起作用

ios - 如何控制 UIPastboard 中的 UIImage 大小?

ios - 带有 UIKeyboardType.NumberPad 和非数字文本的 UITextField

javascript - 如何链接 CSS 和 Jade?

javascript - 将数据添加到 mongodb 时 Node js 中的错误处理