From 220ff08b07068af7c418fd8088458fc8c34ed0a9 Mon Sep 17 00:00:00 2001 From: Philipp Beck Date: Mon, 5 Oct 2020 12:50:11 +0200 Subject: [PATCH] WIP: Get notifications --- .../adobe/phonegap/push/PushConstants.java | 1 + .../com/adobe/phonegap/push/PushPlugin.java | 13 +++++++++++++ src/ios/PushPlugin.m | 10 ++++++++++ src/js/push.js | 19 +++++++++++++++++++ www/push.js | 18 ++++++++++++++++++ 5 files changed, 61 insertions(+) diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 0235b2d73..0703a1463 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -102,5 +102,6 @@ public interface PushConstants { public static final String ONGOING = "ongoing"; public static final String LIST_CHANNELS = "listChannels"; public static final String CLEAR_NOTIFICATION = "clearNotification"; + public static final String GET_NOTIFICATIONS = "getNotifications"; public static final String MESSAGE_ID = "google.message_id"; } diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 492f74781..eb68b01cb 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -460,6 +460,13 @@ public void run () { } } }); + } else if (GET_NOTIFICATIONS.equals(action)) { + cordova.getThreadPool().execute(new Runnable() { + public void run () { + Log.v(LOG_TAG, "getNotifications"); + callbackContext.success(getNotifications(getApplicationContext())); + } + }); } else { Log.e(LOG_TAG, "Invalid action : " + action); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION)); @@ -573,6 +580,12 @@ private void clearNotification (int id) { notificationManager.cancel(appName, id); } + private StatusBarNotification[] getNotifications () { + final NotificationManager notificationManager = (NotificationManager) cordova.getActivity() + .getSystemService(Context.NOTIFICATION_SERVICE); + return notificationManager.getActiveNotifications(); + } + private void subscribeToTopics (JSONArray topics, String registrationToken) { if (topics != null) { String topic = null; diff --git a/src/ios/PushPlugin.m b/src/ios/PushPlugin.m index 5c7602767..7404568e0 100644 --- a/src/ios/PushPlugin.m +++ b/src/ios/PushPlugin.m @@ -522,6 +522,16 @@ - (void)clearNotification:(CDVInvokedUrlCommand *)command }]; } +- (void)getNotifications:(CDVInvokedUrlCommand *)command +{ + [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray * _Nonnull notifications) { + NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1]; + [message setObject:notifications forKey:@"notifications"]; + CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message]; + [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId]; + }]; +} + - (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command { NSMutableDictionary* options = [command.arguments objectAtIndex:0]; diff --git a/src/js/push.js b/src/js/push.js index 78c65dc05..239485b61 100644 --- a/src/js/push.js +++ b/src/js/push.js @@ -221,6 +221,25 @@ class PushNotification { [idNumber]); } + getNotifications (successCallback = () => {}, errorCallback = () => {}) { + if (typeof errorCallback !== 'function') { + console.log( + 'PushNotification.getNotifications failure: failure parameter not a function' + ); + return; + } + + if (typeof successCallback !== 'function') { + console.log( + 'PushNotification.getNotifications failure: success callback ' + + 'parameter must be a function' + ); + return; + } + + exec(successCallback, errorCallback, 'PushNotification', 'getNotifications', []); + } + /** * Listen for an event. * diff --git a/www/push.js b/www/push.js index 202e7941a..b26b639b0 100644 --- a/www/push.js +++ b/www/push.js @@ -241,6 +241,24 @@ var PushNotification = /*#__PURE__*/function () { exec(successCallback, errorCallback, 'PushNotification', 'clearNotification', [idNumber]); } + }, { + key: "getNotifications", + value: function getNotifications() { + var successCallback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {}; + var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {}; + + if (typeof errorCallback !== 'function') { + console.log('PushNotification.getNotifications failure: failure parameter not a function'); + return; + } + + if (typeof successCallback !== 'function') { + console.log('PushNotification.getNotifications failure: success callback ' + 'parameter must be a function'); + return; + } + + exec(successCallback, errorCallback, 'PushNotification', 'getNotifications', []); + } /** * Listen for an event. *