Hugo
Hugo

Reputation: 2204

How to reset application badge number in react native push notification?

I'm looking reset application badge number in react-native-push-notification when the user opens the app but can't find the method anywhere in the docs, any idea?

Upvotes: 4

Views: 13780

Answers (3)

Pradeep Kumar
Pradeep Kumar

Reputation: 21

Very Simple

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


...
...


  
PushNotification.setApplicationIconBadgeNumber(0); //magic

Upvotes: 1

Dylan w
Dylan w

Reputation: 2896

You can add this to your AppDelegate.m, which will set badge count to 0 when app is opened or goes into the background.

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

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

Or

Within app.js (if using react hooks), will also reset badge count when app becomes active or goes into background

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

Upvotes: 6

Liam
Liam

Reputation: 6743

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 imported from react native

Upvotes: 7

Related Questions