diff --git a/README.md b/README.md index ffa85c3..46e0352 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,15 @@ const handleNotification = notification => { // You can fetch required data here don't do anything with UI case 'active': // App is foreground and notification is received. Show a alert or something. + + // You can also decrease or clear badge count with the help of below methods. + // For now those works on iOS only as for Android we have not added any functionality yet. + // Contributors can help here if they want from Android community. + RNPusherPushNotifications.getApplicationIconBadgeNumber((badgeCount) => { + if (badgeCount > 0) { + RNPusherPushNotifications.setApplicationIconBadgeNumber(badgeCount - 1); + } + }); default: break; } else { diff --git a/android/src/main/java/com/b8ne/RNPusherPushNotifications/RNPusherPushNotificationsModule.java b/android/src/main/java/com/b8ne/RNPusherPushNotifications/RNPusherPushNotificationsModule.java index 17a600d..5d8114c 100644 --- a/android/src/main/java/com/b8ne/RNPusherPushNotifications/RNPusherPushNotificationsModule.java +++ b/android/src/main/java/com/b8ne/RNPusherPushNotifications/RNPusherPushNotificationsModule.java @@ -119,4 +119,13 @@ public void run() { }); } + @ReactMethod + public void getApplicationIconBadgeNumber(final Callback callback) { + //some android guy please help with this. + } + + @ReactMethod + public void setApplicationIconBadgeNumber(int badgeNumber) { + //some android guy please help with this. + } } diff --git a/index.js b/index.js index 1d7b080..146b843 100644 --- a/index.js +++ b/index.js @@ -74,4 +74,18 @@ export default { return DeviceEventEmitter.addListener(eventName, payload => callback(payload)); } }, + getApplicationIconBadgeNumber: (callback) => { + if (Platform.OS === 'ios') { + return RNPusherPushNotifications.getApplicationIconBadgeNumber(callback); + } else { + return RNPusherPushNotifications.getApplicationIconBadgeNumber(callback); + } + }, + setApplicationIconBadgeNumber: (badgeNumber) => { + if (Platform.OS === 'ios') { + RNPusherPushNotifications.setApplicationIconBadgeNumber(badgeNumber); + } else { + RNPusherPushNotifications.setApplicationIconBadgeNumber(badgeNumber); + } + } }; diff --git a/ios/RNPusherPushNotifications.m b/ios/RNPusherPushNotifications.m index fddb22f..5c44252 100644 --- a/ios/RNPusherPushNotifications.m +++ b/ios/RNPusherPushNotifications.m @@ -70,6 +70,19 @@ - (dispatch_queue_t)methodQueue }); } +RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback) { + dispatch_async(dispatch_get_main_queue(), ^{ + NSInteger badgeCount = [[UIApplication sharedApplication] applicationIconBadgeNumber]; + callback([NSNumber numberWithInteger:badgeCount]); + }); +} + +RCT_EXPORT_METHOD(setApplicationIconBadgeNumber:(nonnull NSNumber*)badgeNumber) { + dispatch_async(dispatch_get_main_queue(), ^{ + [UIApplication sharedApplication].applicationIconBadgeNumber = [badgeNumber integerValue]; + }); +} + - (void)handleNotification:(NSDictionary *)userInfo { UIApplicationState state = [UIApplication sharedApplication].applicationState;