是否有可能模拟来自adb shell / am命令行的GCM接收? 我收到一个错误

我试图仿真设备正在通过使用adb和命令行接收GCM推送消息。 我试过这个命令来播放一个GCM意图:

adb shell am broadcast -c com.myapp -a com.google.android.c2dm.intent.RECEIVE -e data "SomeData" 

这触发了“权限拒绝”日志行,虽然:

 09-19 12:23:34.820 725-787/? W/BroadcastQueue﹕ Permission Denial: broadcasting Intent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.myapp] flg=0x10 (has extras) } from null (pid=21244, uid=2000) requires com.google.android.c2dm.permission.SEND due to receiver com.myapp/com.google.android.gcm.GCMBroadcastReceiver 

我清单的相关部分:

 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <application> <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <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> 

有任何想法吗?

编辑/澄清: Push / GCM接收在生产中工作。 我正在寻找更简单的方法来testing更改。

Solutions Collecting From Web of "是否有可能模拟来自adb shell / am命令行的GCM接收? 我收到一个错误"

您需要删除接收者定义中的权限属性,如下所示:

 <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> 

比临时编辑AndroidManifest.xml去除权限检查,并记住稍后恢复它更好,您可以自动禁用debugging版本中的权限检查。

为此,请通过Manifest Merger占位符设置属性:

 <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="${gcmPermissionRequired}"> 

然后在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"] } } 

(注意:以前我使用debugging和释放string资源。事实certificate,Play商店如果使用string资源定义了意图filter权限,则拒绝该应用程序。)

我build议使用命令行curl,因为发送GCM推送就像调用一些REST API一样简单。 请参阅下面的示例shell脚本:

 #!/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\"] }"