我正在为我的本机应用程序开发好友请求功能,如果请求被接受,我想使用云消息通知用户(发送者)。它可以在Android上成功运行,并且我正在设备上收到通知,但它无法在iOS上运行。
这是我的云功能,用于接受朋友的请求:
//======================ACCEPT REQUEST==========================//
exports.onAcceptRequest = functions.https.onCall((data, context) => {
const targetId = data.targetId;
const senderId = data.senderId;
return admin
.firestore()
.collection('Friendships')
.doc()
.set({
targetId: targetId,
senderId: senderId,
timeStamp: new Date(),
})
.then(() => {
console.log('Befriended');
notifySenderId(senderId);
return null;
//NOTIFICATION SENT TO uid2(senderId)
})
.catch(err => console.log('errrrrr', err));
});
function notifySenderId(senderId) {
return admin
.firestore()
.collection('Users')
.doc(`${senderId}`)
.get()
.then(doc => {
var tokens = Object.keys(doc.data().fcmTokens);
console.log('TOKENS: ', tokens);
return tokens;
})
.then(tokens => {
console.log('tokens: ', tokens);
const payload = {
notification: {
title: 'Friend Request Accepted',
body: 'Hi add me to a group',
sound: 'default',
},
};
return admin.messaging().sendToDevice(tokens, payload);
})
.then(() => console.log('notified senderId of Acceptance'))
.catch(err => console.log('err notifying senderId', err));
}
在iOS上,云功能在后端成功执行(如下面的功能日志中所示),但是即使我在设置中允许了通知,也不会在设备上显示通知。
我没有使用任何库来显示通知。我希望FCM自动显示通知消息(适用于android)。
编辑:解决了!我了解到fcmTokens可能会随着时间而变化,更新数据库中的令牌解决了我的问题。
但是我有一个后续问题,我们如何跟踪设备上的令牌更新?
解决了!我了解到fcmTokens可能会随着时间而变化,更新数据库中的令牌解决了我的问题。