How to add test coverage for receiving a GCM notification? Test scenario: my app in background, notification arrives, check that it's displayed, click notification to open app. It works in emulator when I test manually. I got bit by Android Notification Not Showing On API 26 so need a test to detect breakages. I saw this How to detect headsup notification in uiautomator? but it doesn't show how to create a notification for my app.
UPDATE I figured out how to create an intent that is handled by the receiver.
// app already started and in background
Intent intent = new Intent();
intent.setAction("com.google.android.c2dm.intent.RECEIVE");
intent.putExtra("from", "1234567890");
intent.putExtra("message", "Android may be hard, but iOS is even harder");
Context context = InstrumentationRegistry.getInstrumentation().getContext();
context.sendBroadcast(intent);
My receiver and listener:
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!--category tag not required if min SDK 16 or higher-->
</intent-filter>
</receiver>
<service
android:name=".gcm.MyGcmListenerService"
android:permission="com.google.android.c2dm.permission.RECEIVE"
android:exported="true" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>