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..3a6b577f 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..78a8889b 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,30 @@ 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(); + synchronized (notificationReceivedEventCache) { + if( notificationReceivedEventCache.containsKey(notification.getNotificationId()) ) { + return; + } + 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; @@ -468,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) { @@ -530,9 +562,10 @@ public void inAppMessageClicked(OSInAppMessageAction result) { } } - @Override + @Override public void onDestroy() { OneSignal.setNotificationOpenedHandler(null); OneSignal.setNotificationWillShowInForegroundHandler(null); + OneSignalRemoteNotificationHandlerSetter.setRemoteNotificationHandler(null); } }