ios - 即使发送(消息)成功,通知也没有到达 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);
});
});
});
解决方案
//在消息负载中添加通知
var message = {
data: {
score: '850'
},
notification: {
title: "",
body: ""
}
token: registrationToken
};
您的代码通常在 Android 中运行。在 IOS 中,您需要在消息有效负载中附加通知并使用 sendToDevice()。试试这个。您必须使用此有效负载获取分数。会工作
推荐阅读
- css - :not(:firstchild) 仍在选择第一个孩子
- photoshop - Photoshop 脚本:重新链接智能对象
- javascript - 为什么我的卡片轮播上的箭头不起作用?
- reactjs - AntDesign 表单内的自定义输入字段 - “组件正在更改要控制的不受控制的输入。”
- python - 使用 api 在 Binance 上获取未结订单的问题
- dask - 为一个函数提供多个核心而不是 1 个
- google-analytics - 在 Google Analytics - Big Query 中衡量已完成交易和转化率的最佳方法?
- forms - 按照 xaml shell 文档创建 FlyoutHeader 时的问题
- delphi - Delphi 过早发布接口
- java - “使用范围绑定或声明重复模块......范围:@javax.inject.Singleton”Dagger 2