Skip to content

Commit 1285c11

Browse files
committed
Additional refactoring and shown and active property fixes
1 parent 890f712 commit 1285c11

File tree

9 files changed

+37
-25
lines changed

9 files changed

+37
-25
lines changed

OneSignalSDK/app/src/main/java/com/onesignal/example/NotificationExtenderServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public NotificationCompat.Builder extend(NotificationCompat.Builder builder) {
3535
return true;
3636
}
3737

38-
private void printObject(Object obj) {
38+
static void printObject(Object obj) {
3939
for (Field field : obj.getClass().getDeclaredFields()) {
4040
String name = field.getName();
4141
try {
4242
Object value = field.get(obj);
43-
// System.out.printf("Field name: %s, Field value: %s%n", name, value);
43+
System.out.printf("Field name: %s, Field value: %s%n", name, value);
4444
} catch (Throwable t){}
4545
}
4646
}

OneSignalSDK/app/src/main/java/com/onesignal/example/OneSignalExampleApp.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ public void onCreate() {
2121
OneSignal.startInit(this)
2222
// .autoPromptLocation(true)
2323
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
24+
.setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())
2425
.init();
2526
}
2627

28+
private class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
29+
@Override
30+
public void notificationReceived(OSNotification notification) {
31+
System.out.println("notificationReceived!!!!!!");
32+
NotificationExtenderServiceTest.printObject(notification);
33+
}
34+
}
35+
2736
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
2837
/**
2938
* Callback to implement in your app to handle when a notification is opened from the Android status bar or
@@ -34,8 +43,11 @@ private class ExampleNotificationOpenedHandler implements OneSignal.Notification
3443
*/
3544
@Override
3645
public void notificationOpened(OSNotificationOpenResult openedResult) {
37-
Log.e("OneSignalExample", "body: " + openedResult.notification.payload.body);
38-
Log.e("OneSignalExample", "additional data: " + openedResult.notification.payload.additionalData);
46+
System.out.println("notificationOpened!!!!!!");
47+
NotificationExtenderServiceTest.printObject(openedResult);
48+
49+
// Log.e("OneSignalExample", "body: " + openedResult.notification.payload.body);
50+
// Log.e("OneSignalExample", "additional data: " + openedResult.notification.payload.additionalData);
3951
//Log.e("OneSignalExample", "additionalData: " + additionalData.toString());
4052
}
4153
}

OneSignalSDK/app/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ protected boolean onNotificationProcessing(OSNotificationReceivedResult notifica
475475
throw new NullPointerException();
476476

477477
this.notification = notification;
478-
notificationId = displayNotification(new OverrideSettings()).notificationId;
478+
notificationId = displayNotification(new OverrideSettings()).androidNotificationId;
479479

480480
return true;
481481
}

OneSignalSDK/app/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import org.robolectric.RuntimeEnvironment;
7171
import org.robolectric.Shadows;
7272
import org.robolectric.annotation.Config;
73-
import org.robolectric.shadows.ShadowActivity;
7473
import org.robolectric.shadows.ShadowApplication;
7574
import org.robolectric.shadows.ShadowConnectivityManager;
7675
import org.robolectric.shadows.ShadowLog;
@@ -199,7 +198,7 @@ public void testInitFromApplicationContext() throws Exception {
199198

200199
@Test
201200
public void testOpenFromNotificationWhenAppIsDead() throws Exception {
202-
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
201+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
203202

204203
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
205204

@@ -214,7 +213,7 @@ public void shouldCorrectlyRemoveOpenedHandlerAndFireMissedOnesWhenAddedBack() t
214213
threadAndTaskWait();
215214

216215
OneSignal.removeNotificationOpenedHandler();
217-
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
216+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
218217
Assert.assertNull(notificationOpenedMessage);
219218

220219
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
@@ -249,7 +248,7 @@ public void testOpenFromNotificationWhenAppIsInBackground() throws Exception {
249248
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
250249
Assert.assertNull(notificationOpenedMessage);
251250

252-
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
251+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
253252
Assert.assertEquals("Test Msg", notificationOpenedMessage);
254253
threadWait();
255254
}
@@ -261,7 +260,7 @@ public void testOpeningLauncherActivity() throws Exception {
261260
// From app launching normally
262261
Assert.assertNotNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
263262

264-
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
263+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
265264

266265
Assert.assertNotNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
267266
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
@@ -274,7 +273,7 @@ public void testOpeningLaunchUrl() throws Exception {
274273

275274
// No OneSignal init here to test case where it is located in an Activity.
276275

277-
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), false);
276+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), false);
278277

279278
Intent intent = Shadows.shadowOf(blankActivity).getNextStartedActivity();
280279
Assert.assertEquals("android.intent.action.VIEW", intent.getAction());
@@ -292,7 +291,7 @@ public void testOpeningLaunchUrlWithDisableDefault() throws Exception {
292291

293292
// No OneSignal init here to test case where it is located in an Activity.
294293

295-
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), false);
294+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), false);
296295
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
297296
}
298297

@@ -307,7 +306,7 @@ public void testDisableOpeningLauncherActivityOnNotifiOpen() throws Exception {
307306
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
308307
Assert.assertNull(notificationOpenedMessage);
309308

310-
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
309+
OneSignal.handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
311310

312311
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
313312
Assert.assertEquals("Test Msg", notificationOpenedMessage);

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationBundleProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int Process(Context context, boolean restoring, JSONObject jsonPayload, N
8383
try {
8484
JSONObject jsonObject = new JSONObject(jsonPayload.toString());
8585
jsonObject.put("notificationId", notificationId);
86-
OneSignal.handleNotificationReceivedWhenInFocus(newJsonArray(jsonObject));
86+
OneSignal.handleNotificationReceived(newJsonArray(jsonObject), true, showAsAlert);
8787
} catch(Throwable t) {}
8888
}
8989

@@ -299,7 +299,7 @@ static boolean processBundle(Context context, final Bundle bundle) {
299299
boolean showAsAlert = OneSignal.getInAppAlertNotificationEnabled();
300300
boolean isActive = OneSignal.isAppActive();
301301
boolean hasBody = bundle.getString("alert") != null && !"".equals(bundle.getString("alert"));
302-
boolean display = hasBody &&
302+
final boolean display = hasBody &&
303303
(OneSignal.getNotificationsWhenActiveEnabled()
304304
|| showAsAlert
305305
|| !isActive);
@@ -313,7 +313,7 @@ static boolean processBundle(Context context, final Bundle bundle) {
313313
// Make a new thread to do our OneSignal work on.
314314
new Thread(new Runnable() {
315315
public void run() {
316-
OneSignal.handleNotificationReceivedWhenInFocus(bundleAsJsonArray(bundle));
316+
OneSignal.handleNotificationReceived(bundleAsJsonArray(bundle), false, false);
317317
}
318318
}).start();
319319
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationExtenderService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected final OSNotificationDisplayedResult displayNotification(OverrideSettin
9292

9393
overrideSettings.override(currentBaseOverrideSettings);
9494
osNotificationDisplayedResult = new OSNotificationDisplayedResult();
95-
osNotificationDisplayedResult.notificationId = NotificationBundleProcessor.Process(this, currentlyRestoring, currentJsonPayload, overrideSettings);
95+
osNotificationDisplayedResult.androidNotificationId = NotificationBundleProcessor.Process(this, currentlyRestoring, currentJsonPayload, overrideSettings);
9696
return osNotificationDisplayedResult;
9797
}
9898

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationOpenedProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void processIntent(Context incContext, Intent inIntent) {
9393
writableDb.close();
9494

9595
if (!dismissed)
96-
OneSignal.handleNotificationReceivedWhenInFocus(context, dataArray, inIntent.getBooleanExtra("from_alert", false));
96+
OneSignal.handleNotificationOpen(context, dataArray, inIntent.getBooleanExtra("from_alert", false));
9797
}
9898

9999
private static void addChildNotifications(JSONArray dataArray, String summaryGroup, SQLiteDatabase writableDb) {

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotificationDisplayedResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
package com.onesignal;
2929

3030
public class OSNotificationDisplayedResult {
31-
public int notificationId;
31+
public int androidNotificationId;
3232
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -986,20 +986,21 @@ public void run() {
986986
}
987987
}
988988

989-
// Called when receiving GCM message when app is open, in focus, and
990-
// enableNotificationsWhenActive and enableInAppAlertNotification are false.
991-
static void handleNotificationReceivedWhenInFocus(JSONArray data) {
989+
// Called when receiving GCM/ADM message after it has been displayed.
990+
// Or right when it is received if it is a silent one
991+
// If a NotificationExtenderService is present in the developers app this will not fire for silent notifications.
992+
static void handleNotificationReceived(JSONArray data, boolean displayed, boolean fromAlert) {
992993
notificationOpenedRESTCall(appContext, data);
993994

994995
if (mInitBuilder == null || mInitBuilder.mNotificationReceivedHandler == null)
995996
return;
996997

997-
OSNotificationOpenResult openResult = generateOsNotificationOpenResult(data, false, false);
998+
OSNotificationOpenResult openResult = generateOsNotificationOpenResult(data, displayed, fromAlert);
998999
mInitBuilder.mNotificationReceivedHandler.notificationReceived(openResult.notification);
9991000
}
10001001

1001-
// Called when opening a notification when the app is suspended in the background, from alert type notification, or when it is dead
1002-
public static void handleNotificationReceivedWhenInFocus(Context inContext, JSONArray data, boolean fromAlert) {
1002+
// Called when opening a notification
1003+
public static void handleNotificationOpen(Context inContext, JSONArray data, boolean fromAlert) {
10031004
notificationOpenedRESTCall(inContext, data);
10041005

10051006
boolean urlOpened = false;

0 commit comments

Comments
 (0)