【发布时间】:2020-02-24 05:56:00
【问题描述】:
我正在努力实现对 Firebase Firestore 文档进行更改时出现的推送通知。我正在使用 react-native-firebase 模块。我的谷歌云功能会监听对 firestore 的更改,然后通过 firebase-admin 发送消息。
google 的参考资料说您可以指定一个设备来发送消息:
// This registration token comes from the client FCM SDKs.
var registrationToken = 'YOUR_REGISTRATION_TOKEN';
var message = {
data: {
score: '850',
time: '2:45'
},
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);
});
在我的 react-native 应用程序中,我使用 react-native-firebase 获得了一个令牌:
function getToken() {
let fcmToken = await AsyncStorage.getItem("fcmToken");
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
await AsyncStorage.setItem("fcmToken", fcmToken);
}
}
}
我是否必须将谷歌云消息传递令牌存储在异步存储以外的其他地方,或者有没有办法在我的谷歌云功能中按原样访问它?看来我应该将身份验证令牌存储在 firestore 中并使用云功能访问 firestore。这是最好的方法吗?
【问题讨论】:
标签: javascript firebase firebase-cloud-messaging google-cloud-functions react-native-firebase