我已经实现了react-native-firebase,并使用云消息传递功能将推送通知发送给我们的用户。在更新应用程序之前,它可以很好地工作。从商店更新后,设备将停止接收推送通知。
这是一个React本机应用程序,我在Home组件(身份验证后的第一页(使用react-navigation))的componentDidMount中使用firebase getToken-function。 我以为onTokenRefresh-listener将在更新后更新令牌,但显然不是,因为它只是在以下操作上更新: -应用删除实例ID -应用在新设备上还原 -用户卸载/重新安装应用程序 -用户清除应用数据
// Home-component
componentDidMount(){
this.checkPermission();
}
async checkPermission() {
firebase.messaging().hasPermission()
.then(enabled => {
if (enabled) {
// user has permissions
this.getToken();
this.createNotificationListeners();
} else {
// user doesn't have permission
this.requestPermission();
}
})
.catch(error => {
console.log('Check - Error', error);
});
}
async getToken() {
const value = await AsyncStorage.getItem('fcmToken');
firebase.messaging().getToken().then((fcmToken) => {
if (fcmToken) {
// user has a device token
if(value !== fcmToken){
AsyncStorage.setItem('fcmToken', fcmToken);
this.save_token_to_db(fcmToken);
}
}
})
}
async requestPermission() {
firebase.messaging().requestPermission()
.then(() => {
// User has authorised
this.getToken();
this.createNotificationListeners();
})
}
save_token_to_db = () => {
// just an update-call to our servers
}
async createNotificationListeners() {
this.onTokenRefreshListener =
firebase.messaging().onTokenRefresh(fcmToken => {
this.save_token_to_db(fcmToken);
});
this.notificationDisplayedListener =
firebase.notifications().onNotificationDisplayed((notification:
Notification) => {
});
this.notificationListener =
firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
});
this.notificationOpenedListener =
firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
});
const notificationOpen = await
firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
}
this.messageListener = firebase.messaging().onMessage((message) => {
});
}
componentWillUnmount(){
this.notificationListener();
this.notificationOpenedListener();
this.onTokenRefreshListener();
this.notificationDisplayedListener();
}
我希望onTokenRefreshListener可以在appupdate上收到更新的令牌。但事实并非如此。 然后,我尝试在每个Home组件装载上执行getToken(确保它在每个应用程序加载中仅发生一次,以最大程度地减少调用(不包括此代码)),检查它是否与AsyncStorage中保存的不匹配。但是它仍然无法正常工作。推送通知在重新安装应用程序后有效,但在appupdate上无效。