I\'m trying to simulate as if the device is receiving a GCM push message by using adb and the command line. I\'ve tried this command to broadcast a GCM intent:
Better than temporarily editing AndroidManifest.xml to remove the permission check, and remembering to restore it later, you can automatically disable the permission check in debug builds.
To do this, set the attribute via a Manifest Merger placeholder:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="${gcmPermissionRequired}">
then set the placeholder in build.gradle
:
buildTypes {
debug {
...
manifestPlaceholders = [gcmPermissionRequired: ""] // "" => let the GCM BroadcastReceiver accept Intents from 'adb shell am broadcast'
}
release {
...
manifestPlaceholders = [gcmPermissionRequired: "com.google.android.c2dm.permission.SEND"]
}
}
(Note: Previously I used debug & release string resources. It turns out that the Play store rejects the app if it defines an intent filter permission using a string resource.)
I suggest using command-line curl, as sending GCM pushes is as easy as calling some REST API. See sample shell script below:
#!/bin/bash
REGISTRATION_ID=YOUR_GCM_REGISTRATION_ID
SERVER_KEY=YOUR_SERVER_KEY_FROM_GOOGLE_API_CONSOLE
curl --header "Authorization: key=$SERVER_KEY" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{ \"data\" : {\"foo\": \"bar\"}, \"registration_ids\":[\"$REGISTRATION_ID\"] }"
You need to remove the permission property in your receiver's definition like this:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myapp" />
</intent-filter>
</receiver>
</application>
sudo apt-get install adb
android:permission="com.google.android.c2dm.permission.SEND" >
adb shell am broadcast -c com.myapp -a com.google.android.c2dm.intent.RECEIVE -e data "SomeData"