Skip to content

Commit 890f712

Browse files
committed
Implementing NotificationReceivedHandler
1 parent 366115e commit 890f712

File tree

7 files changed

+131
-69
lines changed

7 files changed

+131
-69
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) {
7373
// .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
7474
// .init();
7575

76-
OneSignal.setInFocusDisplaying(OneSignal.OSDefaultDisplay.InAppAlert);
76+
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplay.InAppAlert);
7777
OneSignal.sendTag("test3", "test7");
7878
//OneSignal.setSubscription(false);
7979

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public static void OneSignalStateSynchronizer_syncUserState(boolean fromSyncServ
6767
OneSignalStateSynchronizer.syncUserState(fromSyncService);
6868
}
6969

70+
public static int NotificationBundleProcessor_Process(Context context, boolean restoring, JSONObject jsonPayload, NotificationExtenderService.OverrideSettings overrideSettings) {
71+
return NotificationBundleProcessor.Process(context, restoring, jsonPayload, overrideSettings);
72+
}
7073

7174
public class NotificationTable extends OneSignalDbContract.NotificationTable { }
7275
public class NotificationRestorer extends com.onesignal.NotificationRestorer { }

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

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import android.os.Bundle;
4141

4242
import com.onesignal.BuildConfig;
43+
import com.onesignal.OSNotification;
4344
import com.onesignal.OSNotificationOpenResult;
4445
import com.onesignal.OneSignal;
4546
import com.onesignal.OneSignalDbHelper;
@@ -69,6 +70,7 @@
6970
import org.robolectric.RuntimeEnvironment;
7071
import org.robolectric.Shadows;
7172
import org.robolectric.annotation.Config;
73+
import org.robolectric.shadows.ShadowActivity;
7274
import org.robolectric.shadows.ShadowApplication;
7375
import org.robolectric.shadows.ShadowConnectivityManager;
7476
import org.robolectric.shadows.ShadowLog;
@@ -82,6 +84,9 @@
8284
import java.util.List;
8385
import java.util.Map;
8486

87+
import static com.onesignal.OneSignalPackagePrivateHelper.GcmBroadcastReceiver_processBundle;
88+
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationBundleProcessor_Process;
89+
import static com.onesignal.OneSignalPackagePrivateHelper.bundleAsJSONObject;
8590
import static com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle;
8691

8792
@Config(packageName = "com.onesignal.example",
@@ -194,7 +199,7 @@ public void testInitFromApplicationContext() throws Exception {
194199

195200
@Test
196201
public void testOpenFromNotificationWhenAppIsDead() throws Exception {
197-
OneSignal.handleNotificationOpened(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
202+
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
198203

199204
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
200205

@@ -209,7 +214,7 @@ public void shouldCorrectlyRemoveOpenedHandlerAndFireMissedOnesWhenAddedBack() t
209214
threadAndTaskWait();
210215

211216
OneSignal.removeNotificationOpenedHandler();
212-
OneSignal.handleNotificationOpened(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
217+
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);
213218
Assert.assertNull(notificationOpenedMessage);
214219

215220
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
@@ -244,7 +249,7 @@ public void testOpenFromNotificationWhenAppIsInBackground() throws Exception {
244249
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
245250
Assert.assertNull(notificationOpenedMessage);
246251

247-
OneSignal.handleNotificationOpened(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
252+
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false);
248253
Assert.assertEquals("Test Msg", notificationOpenedMessage);
249254
threadWait();
250255
}
@@ -256,7 +261,7 @@ public void testOpeningLauncherActivity() throws Exception {
256261
// From app launching normally
257262
Assert.assertNotNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
258263

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

261266
Assert.assertNotNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
262267
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
@@ -269,7 +274,7 @@ public void testOpeningLaunchUrl() throws Exception {
269274

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

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

274279
Intent intent = Shadows.shadowOf(blankActivity).getNextStartedActivity();
275280
Assert.assertEquals("android.intent.action.VIEW", intent.getAction());
@@ -287,7 +292,7 @@ public void testOpeningLaunchUrlWithDisableDefault() throws Exception {
287292

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

290-
OneSignal.handleNotificationOpened(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), false);
295+
OneSignal.handleNotificationReceivedWhenInFocus(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), false);
291296
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
292297
}
293298

@@ -302,30 +307,59 @@ public void testDisableOpeningLauncherActivityOnNotifiOpen() throws Exception {
302307
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
303308
Assert.assertNull(notificationOpenedMessage);
304309

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

307312
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedActivity());
308313
Assert.assertEquals("Test Msg", notificationOpenedMessage);
309314
}
310315

316+
private static String notificationReceivedBody;
317+
private static int androidNotificationId;
311318
@Test
312319
public void testNotificationReceivedWhenAppInFocus() throws Exception {
313-
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler());
320+
OneSignal.init(blankActivity, "123456789", ONESIGNAL_APP_ID, getNotificationOpenedHandler(), new OneSignal.NotificationReceivedHandler() {
321+
@Override
322+
public void notificationReceived(OSNotification notification) {
323+
androidNotificationId = notification.androidNotificationId;
324+
notificationReceivedBody = notification.payload.body;
325+
}
326+
});
314327
threadAndTaskWait();
328+
329+
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplay.Notification);
330+
331+
Bundle bundle = getBaseNotifBundle();
332+
boolean processResult = GcmBroadcastReceiver_processBundle(blankActivity, bundle);
333+
threadAndTaskWait(); threadAndTaskWait();
334+
Assert.assertEquals(null, notificationOpenedMessage);
335+
Assert.assertFalse(processResult);
336+
// NotificationBundleProcessor.Process(...) will be called if processResult is true as a service
337+
NotificationBundleProcessor_Process(blankActivity, false, bundleAsJSONObject(bundle), null);
338+
Assert.assertEquals("Robo test message", notificationReceivedBody);
339+
Assert.assertFalse(0 == androidNotificationId);
340+
341+
// Don't fire for duplicates
342+
notificationOpenedMessage = null;
343+
notificationReceivedBody = null;
344+
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplay.None);
315345
Assert.assertNull(notificationOpenedMessage);
316346

317-
OneSignalPackagePrivateHelper.GcmBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
347+
GcmBroadcastReceiver_processBundle(blankActivity, bundle);
318348
threadAndTaskWait();
319349
Assert.assertEquals(null, notificationOpenedMessage);
350+
Assert.assertEquals(null, notificationReceivedBody);
320351

352+
// Test that only NotificationReceivedHandler fires
353+
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplay.None);
354+
bundle = getBaseNotifBundle("UUID2");
321355
notificationOpenedMessage = null;
356+
notificationReceivedBody = null;
322357

323-
OneSignal.setInFocusDisplaying(OneSignal.OSDefaultDisplay.NONE);
358+
GcmBroadcastReceiver_processBundle(blankActivity, bundle);
359+
threadAndTaskWait(); threadAndTaskWait();
360+
Assert.assertEquals(null, notificationOpenedMessage);
324361
Assert.assertNull(notificationOpenedMessage);
325-
326-
OneSignalPackagePrivateHelper.GcmBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
327-
threadAndTaskWait();
328-
Assert.assertEquals("Robo test message", notificationOpenedMessage);
362+
Assert.assertEquals("Robo test message", notificationReceivedBody);
329363
}
330364

331365
@Test

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public class GcmIntentService extends IntentService {
4646
public GcmIntentService() {
4747
super("GcmIntentService");
4848
}
49-
49+
50+
// Called when GCM message is received from Google or a notification is being restored.
51+
// Even for ADM messages.
52+
// Expect if a NotificationExtenderService is setup.
5053
@Override
5154
protected void onHandleIntent(Intent intent) {
5255
Bundle extras = intent.getExtras();

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,16 @@ static int Process(Context context, boolean restoring, JSONObject jsonPayload, N
7777
notificationId = new Random().nextInt();
7878

7979
GenerateNotification.fromJsonPayload(context, restoring, notificationId, jsonPayload, showAsAlert && OneSignal.isAppActive(), overrideSettings);
80-
if (!restoring)
80+
81+
if (!restoring) {
8182
saveNotification(context, jsonPayload, false, notificationId);
83+
try {
84+
JSONObject jsonObject = new JSONObject(jsonPayload.toString());
85+
jsonObject.put("notificationId", notificationId);
86+
OneSignal.handleNotificationReceivedWhenInFocus(newJsonArray(jsonObject));
87+
} catch(Throwable t) {}
88+
}
89+
8290
return notificationId;
8391
}
8492

@@ -273,43 +281,49 @@ private static void setBackgroundImageLayout(OSNotificationPayload notification,
273281
}
274282
}
275283

284+
// Return true to count the payload as processed.
276285
static boolean processBundle(Context context, final Bundle bundle) {
277286
// Not a OneSignal GCM message
278287
if (OneSignal.getNotificationIdFromGCMBundle(bundle) == null)
279288
return true;
280289

281290
prepareBundle(bundle);
282291

283-
boolean showAsAlert = OneSignal.getInAppAlertNotificationEnabled();
284-
boolean isActive = OneSignal.isAppActive();
285-
boolean display = OneSignal.getNotificationsWhenActiveEnabled()
286-
|| showAsAlert
287-
|| !isActive;
288-
289292
Intent overrideIntent = NotificationExtenderService.getIntent(context);
290293
if (overrideIntent != null) {
291294
overrideIntent.putExtra("json_payload", bundleAsJSONObject(bundle).toString());
292295
WakefulBroadcastReceiver.startWakefulService(context, overrideIntent);
293296
return true;
294297
}
295298

296-
if (bundle.getString("alert") == null || "".equals(bundle.getString("alert")))
297-
return true;
299+
boolean showAsAlert = OneSignal.getInAppAlertNotificationEnabled();
300+
boolean isActive = OneSignal.isAppActive();
301+
boolean hasBody = bundle.getString("alert") != null && !"".equals(bundle.getString("alert"));
302+
boolean display = hasBody &&
303+
(OneSignal.getNotificationsWhenActiveEnabled()
304+
|| showAsAlert
305+
|| !isActive);
298306

307+
// Save as a opened notification to prevent duplicates.
299308
if (!display) {
309+
if (OneSignal.notValidOrDuplicated(context, bundleAsJSONObject(bundle)))
310+
return true;
311+
saveNotification(context, bundle, true, -1);
300312
// Current thread is meant to be short lived.
301313
// Make a new thread to do our OneSignal work on.
302314
new Thread(new Runnable() {
303315
public void run() {
304-
OneSignal.handleNotificationOpened(bundleAsJsonArray(bundle));
316+
OneSignal.handleNotificationReceivedWhenInFocus(bundleAsJsonArray(bundle));
305317
}
306318
}).start();
307-
308-
// Save as a opened notification to prevent duplicates.
309-
saveNotification(context, bundle, true, -1);
310-
return true;
311319
}
312320

313-
return false;
321+
return !display;
322+
}
323+
324+
static JSONArray newJsonArray(JSONObject jsonObject) {
325+
JSONArray jsonArray = new JSONArray();
326+
jsonArray.put(jsonObject);
327+
return jsonArray;
314328
}
315329
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ static void processIntent(Context incContext, Intent inIntent) {
7373
try {
7474
JSONObject jsonData = new JSONObject(intent.getStringExtra("onesignal_data"));
7575
jsonData.put("notificationId", inIntent.getIntExtra("notificationId", 0));
76-
dataArray = newJsonArray(new JSONObject(intent.getStringExtra("onesignal_data")));
76+
intent.putExtra("onesignal_data", jsonData.toString());
77+
dataArray = NotificationBundleProcessor.newJsonArray(new JSONObject(intent.getStringExtra("onesignal_data")));
7778
} catch (Throwable t) {
7879
t.printStackTrace();
7980
}
@@ -92,7 +93,7 @@ static void processIntent(Context incContext, Intent inIntent) {
9293
writableDb.close();
9394

9495
if (!dismissed)
95-
OneSignal.handleNotificationOpened(context, dataArray, inIntent.getBooleanExtra("from_alert", false));
96+
OneSignal.handleNotificationReceivedWhenInFocus(context, dataArray, inIntent.getBooleanExtra("from_alert", false));
9697
}
9798

9899
private static void addChildNotifications(JSONArray dataArray, String summaryGroup, SQLiteDatabase writableDb) {
@@ -178,9 +179,4 @@ private static ContentValues newContentValuesWithConsumed() {
178179
return values;
179180
}
180181

181-
private static JSONArray newJsonArray(JSONObject jsonObject) {
182-
JSONArray jsonArray = new JSONArray();
183-
jsonArray.put(jsonObject);
184-
return jsonArray;
185-
}
186182
}

0 commit comments

Comments
 (0)