diff --git a/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java b/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java index 6cd354999f3f40a3822396e36bcfa8e7b69615fb..79f0f16dc7bd34bbed1406da0335359129fdedf0 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java @@ -253,24 +253,13 @@ public class FIRLocalMessagingHelper { } public void cancelLocalNotification(String notificationId) { - NotificationManager notificationManager = - (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - - notificationManager.cancel(notificationId.hashCode()); - cancelAlarm(notificationId); - SharedPreferences.Editor editor = sharedPreferences.edit(); editor.remove(notificationId); editor.apply(); } public void cancelAllLocalNotifications() { - NotificationManager notificationManager = - (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - - notificationManager.cancelAll(); - java.util.Map keyMap = sharedPreferences.getAll(); SharedPreferences.Editor editor = sharedPreferences.edit(); for(java.util.Map.Entry entry:keyMap.entrySet()){ @@ -280,6 +269,16 @@ public class FIRLocalMessagingHelper { editor.apply(); } + public void removeDeliveredNotification(String notificationId){ + NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancel(notificationId.hashCode()); + } + + public void removeAllDeliveredNotifications(){ + NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancelAll(); + } + public void cancelAlarm(String notificationId) { Intent notificationIntent = new Intent(mContext, FIRLocalMessagingPublisher.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, notificationId.hashCode(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index c7446cce02f02ef4d3e961a2205ad4872d2b9d08..e006821427e9bb0826bb74a5c2e253eda937613e 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -95,6 +95,16 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li mFIRLocalMessagingHelper.cancelAllLocalNotifications(); } + @ReactMethod + public void removeDeliveredNotification(String notificationID) { + mFIRLocalMessagingHelper.removeDeliveredNotification(notificationID); + } + + @ReactMethod + public void removeAllDeliveredNotifications(){ + mFIRLocalMessagingHelper.removeAllDeliveredNotifications(); + } + @ReactMethod public void subscribeToTopic(String topic){ FirebaseMessaging.getInstance().subscribeToTopic(topic); diff --git a/index.js b/index.js index 5e557619f95141d311c8f80c4ffb9ef4e9aad65b..34ca3758b363d0f03e1f908b30edfe4e4366d3e1 100644 --- a/index.js +++ b/index.js @@ -39,13 +39,27 @@ FCM.getScheduledLocalNotifications = function() { }; FCM.cancelLocalNotification = (notificationID) => { - RNFIRMessaging.cancelLocalNotification(notificationID); + if(!notificationID){ + return; + } + RNFIRMessaging.cancelLocalNotification(notificationID); }; FCM.cancelAllLocalNotifications = () => { RNFIRMessaging.cancelAllLocalNotifications(); }; +FCM.removeDeliveredNotification = (notificationID) => { + if(!notificationID){ + return; + } + RNFIRMessaging.removeDeliveredNotification(notificationID); +} + +FCM.removeAllDeliveredNotifications = () => { + RNFIRMessaging.removeAllDeliveredNotifications(); +} + FCM.setBadgeNumber = (number) => { RNFIRMessaging.setBadgeNumber(number); } diff --git a/ios/RNFIRMesssaging.m b/ios/RNFIRMesssaging.m index 6bc41a0477004d617f2f6dd9ee21179f66d96ab0..75685f42e65a67b8157ffdbc61b027cf11f74f14 100644 --- a/ios/RNFIRMesssaging.m +++ b/ios/RNFIRMesssaging.m @@ -276,13 +276,27 @@ RCT_EXPORT_METHOD(scheduleLocalNotification:(id)data resolver:(RCTPromiseResolve } } +RCT_EXPORT_METHOD(removeDeliveredNotifications:(NSString*) notificationId) +{ + if([UNUserNotificationCenter currentNotificationCenter] != nil){ + [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[notificationId]]; + } +} + +RCT_EXPORT_METHOD(removeAllDeliveredNotifications) +{ + if([UNUserNotificationCenter currentNotificationCenter] != nil){ + [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications]; + } else { + [RCTSharedApplication() setApplicationIconBadgeNumber: 0]; + } +} + RCT_EXPORT_METHOD(cancelAllLocalNotifications) { if([UNUserNotificationCenter currentNotificationCenter] != nil){ [[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests]; - [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications]; } else { - [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; [RCTSharedApplication() cancelAllLocalNotifications]; } }