From 6948e72e91962224a51c14619ee0811ea0c74d31 Mon Sep 17 00:00:00 2001 From: Mahesh Date: Sun, 27 Mar 2022 18:25:44 +0530 Subject: [PATCH 1/2] Added method to control push notification badge count in iOS. You can pass 0 to clear. --- README.md | 9 +++++++++ .../RNPusherPushNotificationsModule.java | 9 +++++++++ index.js | 14 ++++++++++++++ ios/RNPusherPushNotifications.m | 13 +++++++++++++ 4 files changed, 45 insertions(+) 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..eff7dd6 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(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; From b7d473c72830d12ca1b4b29148ca851e347f994b Mon Sep 17 00:00:00 2001 From: Mahesh Date: Sun, 27 Mar 2022 18:41:31 +0530 Subject: [PATCH 2/2] corrected an error. --- ios/RNPusherPushNotifications.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/RNPusherPushNotifications.m b/ios/RNPusherPushNotifications.m index eff7dd6..5c44252 100644 --- a/ios/RNPusherPushNotifications.m +++ b/ios/RNPusherPushNotifications.m @@ -73,7 +73,7 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback) { dispatch_async(dispatch_get_main_queue(), ^{ NSInteger badgeCount = [[UIApplication sharedApplication] applicationIconBadgeNumber]; - callback(badgeCount); + callback([NSNumber numberWithInteger:badgeCount]); }); }