我正在尝试构建Firebase云功能来发送通知。我可以使用FCM令牌通过控制台将通知直接发送到iOS设备,并且我的iOS设备显示notificatino。但是,即使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);
});
});
});
答案 0 :(得分:0)
//在消息有效负载中添加通知
var message = {
data: {
score: '850'
},
notification: {
title: "",
body: ""
}
token: registrationToken
};
您的代码通常适用于Android。在IOS中,您需要在消息有效负载中附加通知,还需要使用sendToDevice()。试试这个。您必须使用此有效负载来获取分数。会工作的