Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Add data/background message support #876

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="onesignal-cordova-plugin"
version="3.3.1">
version="3.3.1-patched">

<name>OneSignal Push Notifications</name>
<author>Josh Kasten, Bradley Hesse, Rodrigo Gomez-Palacio</author>
Expand Down Expand Up @@ -46,6 +46,7 @@
<source-file src="src/android/com/onesignal/cordova/OneSignalObserverController.java" target-dir="src/com/onesignal/cordova/" />
<source-file src="src/android/com/onesignal/cordova/OneSignalOutcomeController.java" target-dir="src/com/onesignal/cordova/" />
<source-file src="src/android/com/onesignal/cordova/OneSignalInAppMessagingController.java" target-dir="src/com/onesignal/cordova/" />
<source-file src="src/android/com/onesignal/OneSignalRemoteNotificationHandlerSetter.java" target-dir="src/com/onesignal/" />
<source-file src="src/android/com/onesignal/cordova/CallbackHelper.java" target-dir="src/com/onesignal/cordova/" />
</platform>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.onesignal;

public class OneSignalRemoteNotificationHandlerSetter {
public static void setRemoteNotificationHandler(OneSignal.OSRemoteNotificationReceivedHandler handler) {
OneSignal.setRemoteNotificationReceivedHandler(handler);
}
}
37 changes: 35 additions & 2 deletions src/android/com/onesignal/cordova/OneSignalPush.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.onesignal.cordova;

import android.content.Context;
import android.util.Log;

import com.onesignal.OSInAppMessageAction;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -530,9 +562,10 @@ public void inAppMessageClicked(OSInAppMessageAction result) {
}
}

@Override
@Override
public void onDestroy() {
OneSignal.setNotificationOpenedHandler(null);
OneSignal.setNotificationWillShowInForegroundHandler(null);
OneSignalRemoteNotificationHandlerSetter.setRemoteNotificationHandler(null);
}
}