如何重置应用程序徽章号码在React Native推送通知?

zqry0prt  于 2022-12-19  发布在  React
关注(0)|答案(3)|浏览(129)

当用户打开应用程序但在文档中找不到方法时,我在react-native-push-notification中寻找重置 * 应用程序徽章编号 *,有什么想法吗?

eufgjt7s

eufgjt7s1#

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
        }
    });

PushNotificationIOS已从react本地导入

cyvaqqii

cyvaqqii2#

您可以将其添加到AppDelegate.m中,当应用打开或进入后台时,它会将徽章计数设置为0。

- (void)applicationDidBecomeActive:(UIApplication *)application{
   [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
 }

-(void)applicationDidEnterBackground:(UIApplication *)application{
   [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
 }

或者
在app.js中(如果使用react钩子),当应用程序处于活动状态或进入后台时,也将重置徽章计数

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);
    }
  }
aiazj4mn

aiazj4mn3#

非常简单

import PushNotification from 'react-native-push-notification';

...
...

  
PushNotification.setApplicationIconBadgeNumber(0); //magic

相关问题