From fcf12b9268d404b35588165ecc468aa47c803273 Mon Sep 17 00:00:00 2001 From: "Andrew Tremblay (Pear profile)" <43054023+andrewtremblay-pear@users.noreply.github.com> Date: Thu, 16 May 2019 09:42:02 -0400 Subject: [PATCH 01/14] typo --- submitting-a-pull-request.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/submitting-a-pull-request.md b/submitting-a-pull-request.md index 53e46e7ce..8611b5230 100644 --- a/submitting-a-pull-request.md +++ b/submitting-a-pull-request.md @@ -23,7 +23,7 @@ If you are testing someones PR, please provide the following feedback: * are you happy that the change in behaviour works as described, and you have tested the change on all affected operating systems? * in your opinion, is the code written _sensibly_? * is it clean and tidy? - * there are and unnecessary changes? + * there are any unnecessary changes? * is it documented appropriately? - \ No newline at end of file + From f0406bee16afcee2d97a5d4b86a81f5911407b26 Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Mon, 25 May 2020 10:05:44 +0200 Subject: [PATCH 02/14] Fix changelog, wrong issue mentioned. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22a882392..b0e8773c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - (Android) Sounds are playing even in Do Not Disturb [#1432](https://github.com/zo0r/react-native-push-notification/issues/1432#issuecomment-633367111) - (Android) onNotification fires every time when the app goes from background to foreground [#1455](https://github.com/zo0r/react-native-push-notification/issues/1455) -- (Android) Cannot send to notification centre because there is no 'message' field in: Bundle [#1452](https://github.com/zo0r/react-native-push-notification/issues/1452) +- (Android) java.lang.NullPointerException: Attempt to invoke virtual method 'void com.dieam.reactnativepushnotification.modules.d.c(android.os.Bundle)' on a null object reference [#1431](https://github.com/zo0r/react-native-push-notification/issues/1431#issuecomment-633315150) ## [3.5.1] - 2020-05-20 From 3ecb5ec1273dc121835cf9e26c5d2339a1919935 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Fri, 5 Jun 2020 15:43:53 +0300 Subject: [PATCH 03/14] getScheduledLocalNotifications for iOS and android --- .../modules/RNPushNotification.java | 10 +- .../modules/RNPushNotificationHelper.java | 189 ++++++++++-------- component/index.android.js | 25 ++- index.js | 16 +- 4 files changed, 138 insertions(+), 102 deletions(-) diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java index 4ceb65d5c..f9d3e6805 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java @@ -250,7 +250,15 @@ public void removeAllDeliveredNotifications() { * Returns a list of all notifications currently in the Notification Center */ public void getDeliveredNotifications(Callback callback) { - callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications()); + callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications()); + } + + @ReactMethod + /** + * Returns a list of all currently scheduled notifications + */ + public void getScheduledLocalNotifications(Callback callback) { + callback.invoke(mRNPushNotificationHelper.getScheduledLocalNotifications()); } @ReactMethod diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index 84bb493ee..2445284a5 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -24,6 +24,7 @@ import android.os.Bundle; import android.service.notification.StatusBarNotification; import android.util.Log; + import androidx.core.app.NotificationCompat; import com.facebook.react.bridge.Arguments; @@ -40,7 +41,7 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.List; -import java.util.Set; +import java.util.Map; import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG; import static com.dieam.reactnativepushnotification.modules.RNPushNotificationAttributes.fromJson; @@ -143,14 +144,14 @@ public void sendNotificationScheduledCore(Bundle bundle) { // notification to the user PendingIntent pendingIntent = toScheduleNotificationIntent(bundle); - if(pendingIntent == null) { + if (pendingIntent == null) { return; } Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s", bundle.getString("id"), Long.toString(fireDate))); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - if(allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent); } else { getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent); @@ -195,7 +196,7 @@ public void sendToNotificationCentre(Bundle bundle) { final String priorityString = bundle.getString("priority"); if (priorityString != null) { - switch(priorityString.toLowerCase()) { + switch (priorityString.toLowerCase()) { case "max": priority = NotificationCompat.PRIORITY_MAX; break; @@ -217,35 +218,35 @@ public void sendToNotificationCentre(Bundle bundle) { } int importance = NotificationManager.IMPORTANCE_HIGH; - final String importanceString = bundle.getString("importance"); - - if (importanceString != null) { - switch(importanceString.toLowerCase()) { - case "default": - importance = NotificationManager.IMPORTANCE_DEFAULT; - break; - case "max": - importance = NotificationManager.IMPORTANCE_MAX; - break; - case "high": - importance = NotificationManager.IMPORTANCE_HIGH; - break; - case "low": - importance = NotificationManager.IMPORTANCE_LOW; - break; - case "min": - importance = NotificationManager.IMPORTANCE_MIN; - break; - case "none": - importance = NotificationManager.IMPORTANCE_NONE; - break; - case "unspecified": - importance = NotificationManager.IMPORTANCE_UNSPECIFIED; - break; - default: - importance = NotificationManager.IMPORTANCE_HIGH; - } - } + final String importanceString = bundle.getString("importance"); + + if (importanceString != null) { + switch (importanceString.toLowerCase()) { + case "default": + importance = NotificationManager.IMPORTANCE_DEFAULT; + break; + case "max": + importance = NotificationManager.IMPORTANCE_MAX; + break; + case "high": + importance = NotificationManager.IMPORTANCE_HIGH; + break; + case "low": + importance = NotificationManager.IMPORTANCE_LOW; + break; + case "min": + importance = NotificationManager.IMPORTANCE_MIN; + break; + case "none": + importance = NotificationManager.IMPORTANCE_NONE; + break; + case "unspecified": + importance = NotificationManager.IMPORTANCE_UNSPECIFIED; + break; + default: + importance = NotificationManager.IMPORTANCE_HIGH; + } + } channel_id = channel_id + "-" + importance; @@ -253,7 +254,7 @@ public void sendToNotificationCentre(Bundle bundle) { final String visibilityString = bundle.getString("visibility"); if (visibilityString != null) { - switch(visibilityString.toLowerCase()) { + switch (visibilityString.toLowerCase()) { case "private": visibility = NotificationCompat.VISIBILITY_PRIVATE; break; @@ -274,12 +275,12 @@ public void sendToNotificationCentre(Bundle bundle) { .setVisibility(visibility) .setPriority(priority) .setAutoCancel(bundle.getBoolean("autoCancel", true)); - + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // API 26 and higher // Changing Default mode of notification notification.setDefaults(Notification.DEFAULT_LIGHTS); } - + String group = bundle.getString("group"); if (group != null) { @@ -350,9 +351,9 @@ public void sendToNotificationCentre(Bundle bundle) { if (!bundle.containsKey("playSound") || bundle.getBoolean("playSound")) { soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - + String soundName = bundle.getString("soundName"); - + if (soundName != null) { if (!"default".equalsIgnoreCase(soundName)) { @@ -414,7 +415,7 @@ public void sendToNotificationCentre(Bundle bundle) { long vibration = bundle.containsKey("vibration") ? (long) bundle.getDouble("vibration") : DEFAULT_VIBRATION; if (vibration == 0) vibration = DEFAULT_VIBRATION; - + channel_id = channel_id + "-" + vibration; vibratePattern = new long[]{0, vibration}; @@ -480,7 +481,7 @@ public void sendToNotificationCentre(Bundle bundle) { if (!(this.isApplicationInForeground(context) && bundle.getBoolean("ignoreInForeground"))) { Notification info = notification.build(); info.defaults |= Notification.DEFAULT_LIGHTS; - + if (bundle.containsKey("tag")) { String tag = bundle.getString("tag"); notificationManager.notify(tag, notificationID, info); @@ -585,39 +586,59 @@ public void clearNotification(int notificationID) { } public void clearDeliveredNotifications(ReadableArray identifiers) { - NotificationManager notificationManager = notificationManager(); - for (int index = 0; index < identifiers.size(); index++) { - String id = identifiers.getString(index); - Log.i(LOG_TAG, "Removing notification with id " + id); - notificationManager.cancel(Integer.parseInt(id)); - } + NotificationManager notificationManager = notificationManager(); + for (int index = 0; index < identifiers.size(); index++) { + String id = identifiers.getString(index); + Log.i(LOG_TAG, "Removing notification with id " + id); + notificationManager.cancel(Integer.parseInt(id)); + } } public WritableArray getDeliveredNotifications() { - NotificationManager notificationManager = notificationManager(); - StatusBarNotification delivered[] = notificationManager.getActiveNotifications(); - Log.i(LOG_TAG, "Found " + delivered.length + " delivered notifications"); - WritableArray result = Arguments.createArray(); - /* - * stay consistent to the return structure in - * https://facebook.github.io/react-native/docs/pushnotificationios.html#getdeliverednotifications - * but there is no such thing as a 'userInfo' - */ - for (StatusBarNotification notification : delivered) { - Notification original = notification.getNotification(); - Bundle extras = original.extras; - WritableMap notif = Arguments.createMap(); - notif.putString("identifier", "" + notification.getId()); - notif.putString("title", extras.getString(Notification.EXTRA_TITLE)); - notif.putString("body", extras.getString(Notification.EXTRA_TEXT)); - notif.putString("tag", notification.getTag()); - notif.putString("group", original.getGroup()); - result.pushMap(notif); - } - - return result; + NotificationManager notificationManager = notificationManager(); + StatusBarNotification delivered[] = notificationManager.getActiveNotifications(); + Log.i(LOG_TAG, "Found " + delivered.length + " delivered notifications"); + WritableArray result = Arguments.createArray(); + /* + * stay consistent to the return structure in + * https://facebook.github.io/react-native/docs/pushnotificationios.html#getdeliverednotifications + * but there is no such thing as a 'userInfo' + */ + for (StatusBarNotification notification : delivered) { + Notification original = notification.getNotification(); + Bundle extras = original.extras; + WritableMap notif = Arguments.createMap(); + notif.putString("identifier", "" + notification.getId()); + notif.putString("title", extras.getString(Notification.EXTRA_TITLE)); + notif.putString("body", extras.getString(Notification.EXTRA_TEXT)); + notif.putString("tag", notification.getTag()); + notif.putString("group", original.getGroup()); + result.pushMap(notif); + } + + return result; } + + public WritableArray getScheduledLocalNotifications() { + WritableArray scheduled = Arguments.createArray(); + + Map scheduledNotifications = scheduledNotificationsPersistence.getAll(); + for (Map.Entry entry : scheduledNotifications.entrySet()) { + try { + RNPushNotificationAttributes notification = fromJson(entry.getValue().toString()); + WritableMap notificationMap = Arguments.makeNativeMap(notification.toBundle()); + + scheduled.pushMap(notificationMap); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + } + } + + return scheduled; + + } + public void cancelAllScheduledNotifications() { Log.i(LOG_TAG, "Cancelling all notifications"); @@ -650,7 +671,7 @@ private void cancelScheduledNotification(String notificationIDString) { b.putString("id", notificationIDString); PendingIntent pendingIntent = toScheduleNotificationIntent(b); - if(pendingIntent != null) { + if (pendingIntent != null) { getAlarmManager().cancel(pendingIntent); } @@ -686,18 +707,18 @@ private static void commit(SharedPreferences.Editor editor) { } public void checkOrCreateDefaultChannel() { - NotificationManager manager = notificationManager(); - - int importance = NotificationManager.IMPORTANCE_HIGH; - Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); - - // Instanciate a default channel with default sound. - String channel_id_sound = NOTIFICATION_CHANNEL_ID + "-default-" + importance + "-" + DEFAULT_VIBRATION; - checkOrCreateChannel(manager, channel_id_sound, soundUri, importance, new long[] {0, DEFAULT_VIBRATION}); - - // Instanciate a default channel without sound defined for backward compatibility. - String channel_id_no_sound = NOTIFICATION_CHANNEL_ID + "-" + importance + "-" + DEFAULT_VIBRATION; - checkOrCreateChannel(manager, channel_id_no_sound, null, importance, new long[] {0, DEFAULT_VIBRATION}); + NotificationManager manager = notificationManager(); + + int importance = NotificationManager.IMPORTANCE_HIGH; + Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + + // Instanciate a default channel with default sound. + String channel_id_sound = NOTIFICATION_CHANNEL_ID + "-default-" + importance + "-" + DEFAULT_VIBRATION; + checkOrCreateChannel(manager, channel_id_sound, soundUri, importance, new long[]{0, DEFAULT_VIBRATION}); + + // Instanciate a default channel without sound defined for backward compatibility. + String channel_id_no_sound = NOTIFICATION_CHANNEL_ID + "-" + importance + "-" + DEFAULT_VIBRATION; + checkOrCreateChannel(manager, channel_id_no_sound, null, importance, new long[]{0, DEFAULT_VIBRATION}); } private void checkOrCreateChannel(NotificationManager manager, String channel_id, Uri soundUri, int importance, long[] vibratePattern) { @@ -730,15 +751,15 @@ private void checkOrCreateChannel(NotificationManager manager, String channel_id manager.createNotificationChannel(channel); } } - + private boolean isApplicationInForeground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List processInfos = activityManager.getRunningAppProcesses(); if (processInfos != null) { for (RunningAppProcessInfo processInfo : processInfos) { if (processInfo.processName.equals(context.getPackageName()) - && processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND - && processInfo.pkgList.length > 0) { + && processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND + && processInfo.pkgList.length > 0) { return true; } } diff --git a/component/index.android.js b/component/index.android.js index 13a442ead..e0342e33f 100644 --- a/component/index.android.js +++ b/component/index.android.js @@ -1,18 +1,18 @@ 'use strict'; -var { +let { NativeModules, DeviceEventEmitter, } = require('react-native'); -var RNPushNotification = NativeModules.RNPushNotification; -var _notifHandlers = new Map(); +let RNPushNotification = NativeModules.RNPushNotification; +let _notifHandlers = new Map(); -var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; -var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered'; -var REMOTE_FETCH_EVENT = 'remoteFetch'; +let DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; +let NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered'; +let REMOTE_FETCH_EVENT = 'remoteFetch'; -var NotificationsComponent = function() { +let NotificationsComponent = function() { }; @@ -66,13 +66,13 @@ NotificationsComponent.prototype.checkPermissions = function(callback) { }; NotificationsComponent.prototype.addEventListener = function(type, handler) { - var listener; + let listener; if (type === 'notification') { listener = DeviceEventEmitter.addListener( DEVICE_NOTIF_EVENT, function(notifData) { if (notifData && notifData.dataJSON) { - var data = JSON.parse(notifData.dataJSON); + let data = JSON.parse(notifData.dataJSON); handler(data); } } @@ -89,7 +89,7 @@ NotificationsComponent.prototype.addEventListener = function(type, handler) { REMOTE_FETCH_EVENT, function(notifData) { if (notifData && notifData.dataJSON) { - var notificationData = JSON.parse(notifData.dataJSON) + let notificationData = JSON.parse(notifData.dataJSON) handler(notificationData); } } @@ -100,7 +100,7 @@ NotificationsComponent.prototype.addEventListener = function(type, handler) { }; NotificationsComponent.prototype.removeEventListener = function(type, handler) { - var listener = _notifHandlers.get(type); + let listener = _notifHandlers.get(type); if (!listener) { return; } @@ -123,6 +123,9 @@ NotificationsComponent.prototype.removeAllDeliveredNotifications = function() { NotificationsComponent.prototype.getDeliveredNotifications = function(callback) { RNPushNotification.getDeliveredNotifications(callback); } +NotificationsComponent.prototype.getScheduledLocalNotifications = function(callback) { + RNPushNotification.getScheduledLocalNotifications(callback); +} NotificationsComponent.prototype.removeDeliveredNotifications = function(identifiers) { RNPushNotification.removeDeliveredNotifications(identifiers); } diff --git a/index.js b/index.js index 3dd36a8f9..23f4224b9 100644 --- a/index.js +++ b/index.js @@ -4,14 +4,14 @@ 'use strict'; -var RNNotificationsComponent = require( './component' ); +let RNNotificationsComponent = require( './component' ); -var AppState = RNNotificationsComponent.state; -var RNNotifications = RNNotificationsComponent.component; +let AppState = RNNotificationsComponent.state; +let RNNotifications = RNNotificationsComponent.component; -var Platform = require('react-native').Platform; +let Platform = require('react-native').Platform; -var Notifications = { +let Notifications = { handler: RNNotifications, onRegister: false, onError: false, @@ -269,7 +269,7 @@ Notifications._onNotification = function(data, isFromBackground = null) { finish: (res) => data.finish(res) }); } else { - var notificationData = { + let notificationData = { foreground: ! isFromBackground, finish: () => {}, ...data @@ -385,6 +385,10 @@ Notifications.getDeliveredNotifications = function() { return this.callNative('getDeliveredNotifications', arguments); } +Notifications.getScheduledLocalNotifications = function() { + return this.callNative('getScheduledLocalNotifications', arguments); +} + Notifications.removeDeliveredNotifications = function() { return this.callNative('removeDeliveredNotifications', arguments); } From 50da5a502c06568aa22985bb8c735da355d3459c Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Fri, 5 Jun 2020 15:50:31 +0300 Subject: [PATCH 04/14] remove changes in whitespaces --- .../modules/RNPushNotificationHelper.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index 830403e1d..59b4fab3b 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -692,8 +692,8 @@ public void clearNotification(String tag, int notificationID) { } public void clearDeliveredNotifications(ReadableArray identifiers) { - NotificationManager notificationManager = notificationManager(); - for (int index = 0; index < identifiers.size(); index++) { + NotificationManager notificationManager = notificationManager(); + for (int index = 0; index < identifiers.size(); index++) { String id = identifiers.getString(index); Log.i(LOG_TAG, "Removing notification with id " + id); notificationManager.cancel(Integer.parseInt(id)); @@ -702,26 +702,26 @@ public void clearDeliveredNotifications(ReadableArray identifiers) { @RequiresApi(api = Build.VERSION_CODES.M) public WritableArray getDeliveredNotifications() { - NotificationManager notificationManager = notificationManager(); - StatusBarNotification delivered[] = notificationManager.getActiveNotifications(); - Log.i(LOG_TAG, "Found " + delivered.length + " delivered notifications"); - WritableArray result = Arguments.createArray(); - /* - * stay consistent to the return structure in - * https://facebook.github.io/react-native/docs/pushnotificationios.html#getdeliverednotifications - * but there is no such thing as a 'userInfo' - */ - for (StatusBarNotification notification : delivered) { - Notification original = notification.getNotification(); - Bundle extras = original.extras; - WritableMap notif = Arguments.createMap(); - notif.putString("identifier", "" + notification.getId()); - notif.putString("title", extras.getString(Notification.EXTRA_TITLE)); - notif.putString("body", extras.getString(Notification.EXTRA_TEXT)); - notif.putString("tag", notification.getTag()); - notif.putString("group", original.getGroup()); - result.pushMap(notif); - } + NotificationManager notificationManager = notificationManager(); + StatusBarNotification delivered[] = notificationManager.getActiveNotifications(); + Log.i(LOG_TAG, "Found " + delivered.length + " delivered notifications"); + WritableArray result = Arguments.createArray(); + /* + * stay consistent to the return structure in + * https://facebook.github.io/react-native/docs/pushnotificationios.html#getdeliverednotifications + * but there is no such thing as a 'userInfo' + */ + for (StatusBarNotification notification : delivered) { + Notification original = notification.getNotification(); + Bundle extras = original.extras; + WritableMap notif = Arguments.createMap(); + notif.putString("identifier", "" + notification.getId()); + notif.putString("title", extras.getString(Notification.EXTRA_TITLE)); + notif.putString("body", extras.getString(Notification.EXTRA_TEXT)); + notif.putString("tag", notification.getTag()); + notif.putString("group", original.getGroup()); + result.pushMap(notif); + } return result; From 3822a9b8cf1c93bf7458e51ab1921062f1f5b00f Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Fri, 5 Jun 2020 15:51:35 +0300 Subject: [PATCH 05/14] whitespaces --- .../modules/RNPushNotificationHelper.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index 59b4fab3b..eb821057a 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -694,10 +694,10 @@ public void clearNotification(String tag, int notificationID) { public void clearDeliveredNotifications(ReadableArray identifiers) { NotificationManager notificationManager = notificationManager(); for (int index = 0; index < identifiers.size(); index++) { - String id = identifiers.getString(index); - Log.i(LOG_TAG, "Removing notification with id " + id); - notificationManager.cancel(Integer.parseInt(id)); - } + String id = identifiers.getString(index); + Log.i(LOG_TAG, "Removing notification with id " + id); + notificationManager.cancel(Integer.parseInt(id)); + } } @RequiresApi(api = Build.VERSION_CODES.M) @@ -723,7 +723,7 @@ public WritableArray getDeliveredNotifications() { result.pushMap(notif); } - return result; + return result; } From a74d91984635a7c503136eb58a9f8a6549cca16b Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 13:21:01 +0300 Subject: [PATCH 06/14] format android getScheduledLocalNotifications return object to match iOS return object --- .../modules/RNPushNotificationHelper.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index eb821057a..eb6cc120b 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -28,7 +28,6 @@ import androidx.core.app.NotificationCompat; import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableArray; @@ -37,6 +36,7 @@ import org.json.JSONArray; import org.json.JSONException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -731,10 +731,27 @@ public WritableArray getScheduledLocalNotifications() { WritableArray scheduled = Arguments.createArray(); Map scheduledNotifications = scheduledNotificationsPersistence.getAll(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"); + for (Map.Entry entry : scheduledNotifications.entrySet()) { try { RNPushNotificationAttributes notification = fromJson(entry.getValue().toString()); - WritableMap notificationMap = Arguments.makeNativeMap(notification.toBundle()); + WritableMap notificationMap = Arguments.createMap(); + + long fireDateTimestamp = (long) notification.getFireDate(); + Date date = new java.util.Date(fireDateTimestamp); + String fireDate = formatter.format(date); + + notificationMap.putString("alertTitle", notification.getTitle()); + notificationMap.putString("alertBody", notification.getMessage()); + notificationMap.putString("alertAction", notification.getActions()); + notificationMap.putString("applicationIconBadgeNumber", notification.getNumber()); + notificationMap.putString("fireDate", fireDate); + notificationMap.putString("id", notification.getId()); + notificationMap.putBoolean("remote", false); + notificationMap.putString("repeatInterval", notification.getRepeatType()); + notificationMap.putString("soundName", notification.getSound()); + scheduled.pushMap(notificationMap); } catch (JSONException e) { @@ -743,7 +760,6 @@ public WritableArray getScheduledLocalNotifications() { } return scheduled; - } public void cancelAllScheduledNotifications() { From 0e5ad3493cb0890d56fba5e5c9829dbe689ea16e Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 13:32:50 +0300 Subject: [PATCH 07/14] change naming of notification params returned on android, return date in unix timestamp as before --- .../modules/RNPushNotificationHelper.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index eb6cc120b..6b1d66e29 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -36,7 +36,6 @@ import org.json.JSONArray; import org.json.JSONException; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -731,22 +730,16 @@ public WritableArray getScheduledLocalNotifications() { WritableArray scheduled = Arguments.createArray(); Map scheduledNotifications = scheduledNotificationsPersistence.getAll(); - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"); for (Map.Entry entry : scheduledNotifications.entrySet()) { try { RNPushNotificationAttributes notification = fromJson(entry.getValue().toString()); WritableMap notificationMap = Arguments.createMap(); - long fireDateTimestamp = (long) notification.getFireDate(); - Date date = new java.util.Date(fireDateTimestamp); - String fireDate = formatter.format(date); - - notificationMap.putString("alertTitle", notification.getTitle()); - notificationMap.putString("alertBody", notification.getMessage()); - notificationMap.putString("alertAction", notification.getActions()); - notificationMap.putString("applicationIconBadgeNumber", notification.getNumber()); - notificationMap.putString("fireDate", fireDate); + notificationMap.putString("title", notification.getTitle()); + notificationMap.putString("message", notification.getMessage()); + notificationMap.putString("number", notification.getNumber()); + notificationMap.putString("date", notification.getFireDate()); notificationMap.putString("id", notification.getId()); notificationMap.putBoolean("remote", false); notificationMap.putString("repeatInterval", notification.getRepeatType()); From e207fa8d626d40bb5c95e8b1128a25065729a94f Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 13:54:11 +0300 Subject: [PATCH 08/14] add missing getters for notification attributes --- .../modules/RNPushNotificationAttributes.java | 20 +++++++++++++++++++ .../modules/RNPushNotificationHelper.java | 3 +-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java index 3bacacc96..2ecf82581 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java @@ -330,6 +330,26 @@ public String getId() { return id; } + public String getSound() { + return sound; + } + + public String getMessage() { + return message; + } + + public String getTitle() { + return title; + } + + public String getNumber() { + return number; + } + + public String getRepeatType() { + return repeatType; + } + public double getFireDate() { return fireDate; } diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index 6b1d66e29..0d4cdb362 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -739,13 +739,12 @@ public WritableArray getScheduledLocalNotifications() { notificationMap.putString("title", notification.getTitle()); notificationMap.putString("message", notification.getMessage()); notificationMap.putString("number", notification.getNumber()); - notificationMap.putString("date", notification.getFireDate()); + notificationMap.putDouble("date", notification.getFireDate()); notificationMap.putString("id", notification.getId()); notificationMap.putBoolean("remote", false); notificationMap.putString("repeatInterval", notification.getRepeatType()); notificationMap.putString("soundName", notification.getSound()); - scheduled.pushMap(notificationMap); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage()); From b62b27b16de43a76e8dc298231b8db0dfb380123 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 14:36:16 +0300 Subject: [PATCH 09/14] map scheduled local notifications to united return object for both platforms --- index.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2e897b649..d6d2cdf0b 100644 --- a/index.js +++ b/index.js @@ -439,8 +439,42 @@ Notifications.getDeliveredNotifications = function() { return this.callNative('getDeliveredNotifications', arguments); } -Notifications.getScheduledLocalNotifications = function() { - return this.callNative('getScheduledLocalNotifications', arguments); +Notifications.getScheduledLocalNotifications = function(callback) { + const mapNotifications = (notifications) => { + let mappedNotifications = []; + if(notifications?.length > 0) { + if(Platform.OS === 'ios'){ + mappedNotifications = notifications.map(notif => { + return ({ + soundName: notif.soundName, + repeatInterval: notif.repeatInterval, + remote: notif.remote, + id: notif.userInfo?.id, + date: notif.fireDate, + number: notif?.applicationIconBadgeNumber, + message: notif?.alertBody, + title: notif?.alertTitle, + }) + }) + } else if(Platform.OS === 'android') { + mappedNotifications = notifications.map(notif => { + return ({ + soundName: notif.soundName, + repeatInterval: notif.repeatInterval, + remote: notif.remote, + id: notif.id, + date: notif.date, + number: notif.number, + message: notif.message, + title: notif.title, + }) + }) + } + } + callback(mappedNotifications); + } + + return this.callNative('getScheduledLocalNotifications', [mapNotifications]); } Notifications.removeDeliveredNotifications = function() { From 7c0a381275c2ea1626a1ed95e71040515b6cc62c Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 15:07:55 +0300 Subject: [PATCH 10/14] format date for scheduled notifications object --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d6d2cdf0b..e56716cbd 100644 --- a/index.js +++ b/index.js @@ -450,7 +450,7 @@ Notifications.getScheduledLocalNotifications = function(callback) { repeatInterval: notif.repeatInterval, remote: notif.remote, id: notif.userInfo?.id, - date: notif.fireDate, + date: new Date(notif.fireDate), number: notif?.applicationIconBadgeNumber, message: notif?.alertBody, title: notif?.alertTitle, @@ -463,7 +463,7 @@ Notifications.getScheduledLocalNotifications = function(callback) { repeatInterval: notif.repeatInterval, remote: notif.remote, id: notif.id, - date: notif.date, + date: new Date(notif.date), number: notif.number, message: notif.message, title: notif.title, From d904712d235373b05f9a37a172b8605dd257d9ea Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 15:08:12 +0300 Subject: [PATCH 11/14] bump @react-native-community/push-notification-ios to v1.2.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cc11f1c34..71e9dafe3 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "url": "git+ssh://git@github.com:zo0r/react-native-push-notification.git" }, "dependencies": { - "@react-native-community/push-notification-ios": "^1.2.0" + "@react-native-community/push-notification-ios": "^1.2.1" }, "peerDependencies": { "react-native": ">=0.33" From c4865aaf615704da65d7fc841fe975d439c36bb0 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 16:25:20 +0300 Subject: [PATCH 12/14] add example button to log scheduled notifications --- example/App.js | 7 +++++++ example/NotifService.js | 4 ++++ example/ios/Podfile.lock | 6 +++--- example/package.json | 3 ++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/example/App.js b/example/App.js index 05840d965..a4c8458b0 100644 --- a/example/App.js +++ b/example/App.js @@ -105,6 +105,13 @@ export default class App extends Component { }}> Abandon Permissions + { + this.notif.getScheduledLocalNotifications(notifs => console.log(notifs)); + }}> + Console.Log Scheduled Local Notifications + diff --git a/example/NotifService.js b/example/NotifService.js index d28fca39e..ab9bac75d 100644 --- a/example/NotifService.js +++ b/example/NotifService.js @@ -109,4 +109,8 @@ export default class NotifService { abandonPermissions() { PushNotification.abandonPermissions(); } + + getScheduledLocalNotifications(callback) { + PushNotification.getScheduledLocalNotifications(callback); + } } diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index cc1639da9..509394e20 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -292,7 +292,7 @@ PODS: - React-cxxreact (= 0.62.2) - React-jsi (= 0.62.2) - ReactCommon/callinvoker (= 0.62.2) - - RNCPushNotificationIOS (1.1.1): + - RNCPushNotificationIOS (1.2.1): - React - Yoga (1.14.0) - YogaKit (1.18.1): @@ -453,10 +453,10 @@ SPEC CHECKSUMS: React-RCTText: fae545b10cfdb3d247c36c56f61a94cfd6dba41d React-RCTVibration: 4356114dbcba4ce66991096e51a66e61eda51256 ReactCommon: ed4e11d27609d571e7eee8b65548efc191116eb3 - RNCPushNotificationIOS: a0b6894f4ad9b93d9dac467fdf4d055660ac8a0d + RNCPushNotificationIOS: 5878fb73cb1a8a78c3a1a0834d969b01553836d0 Yoga: 3ebccbdd559724312790e7742142d062476b698e YogaKit: f782866e155069a2cca2517aafea43200b01fd5a PODFILE CHECKSUM: 56c2537f71f3f02200d6918c542a8e89a0b422fa -COCOAPODS: 1.9.1 +COCOAPODS: 1.9.2 diff --git a/example/package.json b/example/package.json index b01e827fe..70f616c0b 100644 --- a/example/package.json +++ b/example/package.json @@ -7,7 +7,8 @@ "ios": "react-native run-ios", "start": "react-native start", "test": "jest", - "lint": "eslint ." + "lint": "eslint .", + "pod-install": "cd ios && pod install" }, "dependencies": { "@react-native-community/push-notification-ios": "^1.1.1", From ee9b9b7d183b189b649002345deeb2641d148bff Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 8 Jun 2020 16:34:59 +0300 Subject: [PATCH 13/14] add docs --- README.md | 26 +++++++++++++++++++ .../modules/RNPushNotificationHelper.java | 1 - index.js | 2 -- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ff207947f..978ac74a2 100644 --- a/README.md +++ b/README.md @@ -524,6 +524,32 @@ Removes the specified notifications from Notification Center | ----------- | ----- | -------- | ---------------------------------- | | identifiers | array | Yes | Array of notification identifiers. | +### 6) getScheduledLocalNotifications + +```javascript +PushNotificationIOS.getScheduledLocalNotifications(callback); +``` + +Provides you with a list of the app’s scheduled local notifications that are yet to be displayed + +**Parameters:** + +| Name | Type | Required | Description | +| -------- | -------- | -------- | ----------------------------------------------------------- | +| callback | function | Yes | Function which receive an array of delivered notifications. | + +Returns an array of local scheduled notification objects containing: + +| Name | Type | Description | +| -------- | -------- | -------- | ----------------------------------------------------------- | +| id | number | The identifier of this notification. | +| date | Date | The fire date of this notification. | +| title | string | The title of this notification. | +| message | string | The message body of this notification. | +| soundName | string | The sound name of this notification. | +| repeatInterval | number | The repeat interval of this notification. | +| number | number | App notification badge count number. | + ## Abandon Permissions `PushNotification.abandonPermissions()` Revokes the current token and unregister for all remote notifications received via APNS or FCM. diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index 0d4cdb362..c8f3a57dd 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -741,7 +741,6 @@ public WritableArray getScheduledLocalNotifications() { notificationMap.putString("number", notification.getNumber()); notificationMap.putDouble("date", notification.getFireDate()); notificationMap.putString("id", notification.getId()); - notificationMap.putBoolean("remote", false); notificationMap.putString("repeatInterval", notification.getRepeatType()); notificationMap.putString("soundName", notification.getSound()); diff --git a/index.js b/index.js index e56716cbd..4aa8cf029 100644 --- a/index.js +++ b/index.js @@ -448,7 +448,6 @@ Notifications.getScheduledLocalNotifications = function(callback) { return ({ soundName: notif.soundName, repeatInterval: notif.repeatInterval, - remote: notif.remote, id: notif.userInfo?.id, date: new Date(notif.fireDate), number: notif?.applicationIconBadgeNumber, @@ -461,7 +460,6 @@ Notifications.getScheduledLocalNotifications = function(callback) { return ({ soundName: notif.soundName, repeatInterval: notif.repeatInterval, - remote: notif.remote, id: notif.id, date: new Date(notif.date), number: notif.number, From 1c7a1965799fce18668ae2f46fd215164b99c717 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Tue, 9 Jun 2020 10:08:57 +0300 Subject: [PATCH 14/14] "@react-native-community/push-notification-ios": "^1.2.2" --- example/ios/Podfile.lock | 4 ++-- example/package.json | 2 +- package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 509394e20..b3adad45f 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -292,7 +292,7 @@ PODS: - React-cxxreact (= 0.62.2) - React-jsi (= 0.62.2) - ReactCommon/callinvoker (= 0.62.2) - - RNCPushNotificationIOS (1.2.1): + - RNCPushNotificationIOS (1.2.2): - React - Yoga (1.14.0) - YogaKit (1.18.1): @@ -453,7 +453,7 @@ SPEC CHECKSUMS: React-RCTText: fae545b10cfdb3d247c36c56f61a94cfd6dba41d React-RCTVibration: 4356114dbcba4ce66991096e51a66e61eda51256 ReactCommon: ed4e11d27609d571e7eee8b65548efc191116eb3 - RNCPushNotificationIOS: 5878fb73cb1a8a78c3a1a0834d969b01553836d0 + RNCPushNotificationIOS: 4c97a36dbec42dba411cc35e6dac25e34a805fde Yoga: 3ebccbdd559724312790e7742142d062476b698e YogaKit: f782866e155069a2cca2517aafea43200b01fd5a diff --git a/example/package.json b/example/package.json index 70f616c0b..606b5d146 100644 --- a/example/package.json +++ b/example/package.json @@ -11,7 +11,7 @@ "pod-install": "cd ios && pod install" }, "dependencies": { - "@react-native-community/push-notification-ios": "^1.1.1", + "@react-native-community/push-notification-ios": "^1.2.2", "react": "16.11.0", "react-native": "0.62.2", "react-native-push-notification": "git+https://git@github.com/zo0r/react-native-push-notification.git" diff --git a/package.json b/package.json index 71e9dafe3..619625482 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "url": "git+ssh://git@github.com:zo0r/react-native-push-notification.git" }, "dependencies": { - "@react-native-community/push-notification-ios": "^1.2.1" + "@react-native-community/push-notification-ios": "^1.2.2" }, "peerDependencies": { "react-native": ">=0.33"