(sorry for my bad English)
When I receive a notification the ios badge increments. When I launch my application and I return to the home the badge has disappeared but if I receive a new notification it does not start from 0 but from the previous number...
In my client during the opening event of the notification I set the badge to 0.
async componentDidMount() {
...
OneSignal.addEventListener('ids', this.onIds)
OneSignal.addEventListener('received', this.onReceived)
OneSignal.addEventListener('opened', this.onOpened)
const chatBadgeNumber = await this.getNumberFromAsyncStorage(CHAT_BADGE_COUNT)
this.setBadgeNumber(chatBadgeNumber)
this.setState({
chatBadgeNumber,
})
}
componentWillUnmount() {
OneSignal.removeEventListener('ids', this.onIds)
OneSignal.removeEventListener('received', this.onReceived)
OneSignal.removeEventListener('opened', this.onOpened)
}
setBadgeNumber = number => {
try {
if (Platform.OS === 'android') {
BadgeAndroid.setBadge(number)
} else if (Platform.OS === 'ios') {
PushNotificationIOS.setApplicationIconBadgeNumber(number)
}
} catch (e) {
// if iOS app is in background, app crash
}
}
onReceived = async notification => {
const { chatBadgeNumber } = this.state
...
const { increaseBadge } = notification.payload.additionalData
if (increaseBadge === 'CHAT') {
this.setChatBadgeNumber(chatBadgeNumber + 1)
}
...
}
onOpened = async notification => {
const { increaseBadge } = notification.notification.groupedNotifications
if (increaseBadge === 'CHAT') {
this.setChatBadgeNumber(0)
...
}
...
}
}
In my AppDelegate.m too :
- (void)applicationDidBecomeActive:(UIApplication *)application{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)sceneDidBecomeActive:(UIScene *)scene{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
"@react-native-community/push-notification-ios": "^1.1.0", "react-native-onesignal": "^3.5.0",
On IOS 12 reset OK, increment KO (one problem at a time...) On Android everything is OK (increment, reset)
I don't know where exactly the problem comes from but if you have an idea ...