Monday, 28 December 2020

React-native android push notifications clear badge

I'm building an app, and I'm using push notifications with firebase. Everything is working fine, except one tiny thing that I don't get. When receiving a notification, badge is incremented. When I open the app via the notification, or directly by clicking on the app if it's closed or in background, badge is set to 0. It works perfectly on iOS, and almost on android. I can't clear the badge if I open the app directly by clicking on it, without going via a notification... Here's my code :

async componentDidMount() {
      this.checkPermission();
      this.createNotificationListeners();
      AppState.addEventListener('change', this._handleAppStateChange);
      firebase.notifications().setBadge(0);
  }

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

   _handleAppStateChange = async (nextAppState) => {
    if (
      this.state.appState.match(/inactive|background/) &&
      nextAppState === 'active'
    ) {
      firebase.notifications().setBadge(0);
    }
  };

Is there something I'm doing wrong ?



from React-native android push notifications clear badge

No comments:

Post a Comment