我想在用户打开应用时重置react-native-push-notification中的应用程序徽章数字,但是在文档中找不到任何方法,请问有什么建议吗?
https://facebook.github.io/react-native/docs/pushnotificationios.html#setapplicationiconbadgenumber
PushNotificationIOS.getApplicationIconBadgeNumber((num)=>{ // get current number
if(num >= 1){
PushNotificationIOS.setApplicationIconBadgeNumber(0) //set number to 0
}
});
从 React Native 引入 PushNotificationIOS
- (void)applicationDidBecomeActive:(UIApplication *)application{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
-(void)applicationDidEnterBackground:(UIApplication *)application{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
或者
在 app.js 中(如果使用 React Hooks),当应用程序变为活动状态或进入后台时,还将重置徽章计数
import { AppState } from 'react-native';
useEffect(() => {
AppState.addEventListener("change", _handleAppStateChange);
return () => {
AppState.removeEventListener("change", _handleAppStateChange);
};
}, []);
const _handleAppStateChange = (nextAppState) => {
if (Platform.OS === 'ios' && nextAppState === 'active' || nextAppState === 'background') {
PushNotificationIOS.setApplicationIconBadgeNumber(0);
}
}
非常简单
import PushNotification from 'react-native-push-notification';
...
...
PushNotification.setApplicationIconBadgeNumber(0); //magic
setApplicationIconBadgeNumber(0)
,参考链接:https://facebook.github.io/react-native/docs/pushnotificationios.html#setapplicationiconbadgenumber - Ivan Chernykh