From 9c0b9df8e14af9ae6660fb5655625a009248fddb Mon Sep 17 00:00:00 2001 From: Anatoly Shirokov Date: Wed, 5 Apr 2023 10:44:40 +0300 Subject: [PATCH 1/4] [Android] Add remoteNotificationHandler to process background message with content_available=true --- package.json | 2 +- plugin.xml | 3 ++- ...SignalRemoteNotificationHandlerSetter.java | 7 ++++++ .../com/onesignal/cordova/OneSignalPush.java | 25 ++++++++++++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/android/com/onesignal/OneSignalRemoteNotificationHandlerSetter.java diff --git a/package.json b/package.json index f27cfa8c..d55cc6f0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "3.3.1", + "version": "3.3.1-patched", "name": "onesignal-cordova-plugin", "cordova_name": "OneSignal Push Notifications", "description": "OneSignal is a high volume Push Notification service for mobile apps. In addition to basic notification delivery, OneSignal also provides tools to localize, target, schedule, and automate notifications that you send.", diff --git a/plugin.xml b/plugin.xml index e23f62bf..058772a4 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="3.3.1-patched"> OneSignal Push Notifications Josh Kasten, Bradley Hesse, Rodrigo Gomez-Palacio @@ -46,6 +46,7 @@ + diff --git a/src/android/com/onesignal/OneSignalRemoteNotificationHandlerSetter.java b/src/android/com/onesignal/OneSignalRemoteNotificationHandlerSetter.java new file mode 100644 index 00000000..0e94db62 --- /dev/null +++ b/src/android/com/onesignal/OneSignalRemoteNotificationHandlerSetter.java @@ -0,0 +1,7 @@ +package com.onesignal; + +public class OneSignalRemoteNotificationHandlerSetter { + public static void setRemoteNotificationHandler(OneSignal.OSRemoteNotificationReceivedHandler handler) { + OneSignal.setRemoteNotificationReceivedHandler(handler); + } +} diff --git a/src/android/com/onesignal/cordova/OneSignalPush.java b/src/android/com/onesignal/cordova/OneSignalPush.java index fddbf0f4..79717cff 100644 --- a/src/android/com/onesignal/cordova/OneSignalPush.java +++ b/src/android/com/onesignal/cordova/OneSignalPush.java @@ -27,6 +27,7 @@ package com.onesignal.cordova; +import android.content.Context; import android.util.Log; import com.onesignal.OSInAppMessageAction; @@ -36,6 +37,7 @@ import com.onesignal.OSNotificationOpenedResult; import com.onesignal.OSNotificationReceivedEvent; import com.onesignal.OneSignal; +import com.onesignal.OneSignalRemoteNotificationHandlerSetter; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; @@ -128,6 +130,7 @@ public class OneSignalPush extends CordovaPlugin { public boolean setNotificationWillShowInForegroundHandler(CallbackContext callbackContext) { OneSignal.setNotificationWillShowInForegroundHandler(new CordovaNotificationInForegroundHandler(callbackContext)); + OneSignalRemoteNotificationHandlerSetter.setRemoteNotificationHandler(new CordovaRemoteNotificationHandler(callbackContext)); return true; } @@ -456,6 +459,26 @@ private boolean completeNotification(JSONArray data) { * Handlers */ + private static class CordovaRemoteNotificationHandler implements OneSignal.OSRemoteNotificationReceivedHandler { + private CallbackContext jsNotificationInForegroundCallBack; + + public CordovaRemoteNotificationHandler(CallbackContext inCallbackContext) { + jsNotificationInForegroundCallBack = inCallbackContext; + } + + @Override + public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent osNotificationReceivedEvent) { + try { + OSNotification notification = osNotificationReceivedEvent.getNotification(); + notificationReceivedEventCache.put(notification.getNotificationId(), osNotificationReceivedEvent); + + CallbackHelper.callbackSuccess(jsNotificationInForegroundCallBack, notification.toJSONObject()); + } catch (Throwable t) { + t.printStackTrace(); + } + } + } + private static class CordovaNotificationInForegroundHandler implements OneSignal.OSNotificationWillShowInForegroundHandler { private CallbackContext jsNotificationInForegroundCallBack; @@ -530,7 +553,7 @@ public void inAppMessageClicked(OSInAppMessageAction result) { } } - @Override + @Override public void onDestroy() { OneSignal.setNotificationOpenedHandler(null); OneSignal.setNotificationWillShowInForegroundHandler(null); From e0cd7779db700ffa90e5112b495ab58af87a7ae8 Mon Sep 17 00:00:00 2001 From: Anatoly Shirokov Date: Wed, 5 Apr 2023 15:09:00 +0300 Subject: [PATCH 2/4] Clear remoteNotificationHandler on destroy --- src/android/com/onesignal/cordova/OneSignalPush.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/android/com/onesignal/cordova/OneSignalPush.java b/src/android/com/onesignal/cordova/OneSignalPush.java index 79717cff..9f3b6059 100644 --- a/src/android/com/onesignal/cordova/OneSignalPush.java +++ b/src/android/com/onesignal/cordova/OneSignalPush.java @@ -557,5 +557,6 @@ public void inAppMessageClicked(OSInAppMessageAction result) { public void onDestroy() { OneSignal.setNotificationOpenedHandler(null); OneSignal.setNotificationWillShowInForegroundHandler(null); + OneSignalRemoteNotificationHandlerSetter.setRemoteNotificationHandler(null); } } From 827f6ce41bf2d39baaf35c967b90fa290804a94b Mon Sep 17 00:00:00 2001 From: Anatoly Shirokov Date: Wed, 5 Apr 2023 15:42:38 +0300 Subject: [PATCH 3/4] Prevent double processing of identical OSNotificationReceivedEvent events --- .../com/onesignal/cordova/OneSignalPush.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/android/com/onesignal/cordova/OneSignalPush.java b/src/android/com/onesignal/cordova/OneSignalPush.java index 9f3b6059..78a8889b 100644 --- a/src/android/com/onesignal/cordova/OneSignalPush.java +++ b/src/android/com/onesignal/cordova/OneSignalPush.java @@ -470,8 +470,12 @@ public CordovaRemoteNotificationHandler(CallbackContext inCallbackContext) { public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent osNotificationReceivedEvent) { try { OSNotification notification = osNotificationReceivedEvent.getNotification(); - notificationReceivedEventCache.put(notification.getNotificationId(), osNotificationReceivedEvent); - + synchronized (notificationReceivedEventCache) { + if( notificationReceivedEventCache.containsKey(notification.getNotificationId()) ) { + return; + } + notificationReceivedEventCache.put(notification.getNotificationId(), osNotificationReceivedEvent); + } CallbackHelper.callbackSuccess(jsNotificationInForegroundCallBack, notification.toJSONObject()); } catch (Throwable t) { t.printStackTrace(); @@ -491,7 +495,12 @@ public CordovaNotificationInForegroundHandler(CallbackContext inCallbackContext) public void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) { try { OSNotification notification = notificationReceivedEvent.getNotification(); - notificationReceivedEventCache.put(notification.getNotificationId(), notificationReceivedEvent); + synchronized (notificationReceivedEventCache) { + if( notificationReceivedEventCache.containsKey(notification.getNotificationId()) ) { + return; + } + notificationReceivedEventCache.put(notification.getNotificationId(), notificationReceivedEvent); + } CallbackHelper.callbackSuccess(jsNotificationInForegroundCallBack, notification.toJSONObject()); } catch (Throwable t) { From a053d76c52ff894c746be74a54be86763585aeaa Mon Sep 17 00:00:00 2001 From: Anatoly Shirokov Date: Wed, 5 Apr 2023 18:45:24 +0300 Subject: [PATCH 4/4] Fix OneSignalRemoteNotificationHandlerSetter.java location --- plugin.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 058772a4..3a6b577f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -46,7 +46,7 @@ - +