I develop some interesting Push notifications in our application and do some tests to cover this when the user clicks on one of the notifications and then the application launches the correct intention to open the correct activity.
In our application, we have StartupActivity, which captures all these push notifications and is sent to the correct screen with the correct additions. A user interface test performed with Espresso, which enables a proper launch, is as follows:
@Test
public void showsANotificationAndOpensTheCorrectScreen() throws
UiObjectNotFoundException {
sendBroadcast(PushNotificationBuilder
.withAction("com.xxxx.android.gcm.SOMETHING")
.withType("SOME_TYPE")
.withRelatedId(ANY_ID)
.withTitle(ANY_TITLE)
.build());
tapNotificationWithTitle(ANY_TITLE);
intended(allOf(
hasComponent(DesitinyActivity.class.getCanonicalName()),
hasExtra("extra_id", Long.valueOf(ANY_ID)),
hasExtra("extra_other_extra", true)));
}
As you can see, this test simulates receiving a notification, click on it and checks if the intent is selected for the correct activity.
, , , , , . TaskStackBuilder PendingIntent . , :
private PendingIntent generateexampleTaskBuilder(Context context, Intent intentToTheFinalScreen) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(ExampleActivity.getLaunchIntent(someExtra, context));
stackBuilder.addNextIntent(intent);
return stackBuilder.getPendingIntent(PushIdIntegerGenerator.getUniquePushId(),
PendingIntent.FLAG_UPDATE_CURRENT);
}
, intended espresso , . , . , , (), .
- ?