Testing Your Android GCM Broadcast Receiver
2015-08-11 00:00
357 查看
Testing Your Android GCM Broadcast Receiver without GCM Messages
So depending on your environment it's kind of a pain-in-the-butt to test Google Cloud Messaging on an actual device.
Wouldn't it be nice if you could just test the code locally that would receive those magical GCM messages? *You can*.
adb shell is a magical place - a place where you can construct intents and fire them off into the system.
Step 1: Open your AndroidManifest.xml, locate your GCM Broadcast Receiver (let's say ours is net.npike.android.gcm.GCMBroadcastReceiver and our package name is net.npike.android)
<receiver
android:name="net.npike.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="net.npike.android"/>
</intent-filter>
</receiver>
Step 2: Temporarily remove the following attribute from its declaration:
android:permission="com.google.android.c2dm.permission.SEND"
Step 3: Open a terminal / command prompt where you can access ADB.
Step 4: Enter the shell.
adb shell
Step 5: Paste the following (replacing the appropriate values) and hit enter:
am broadcast -a com.google.android.c2dm.intent.RECEIVE --es "data.alert" "foo"
If you did everything correctly you should get some output like:
Broadcasting: Intent { act=com.google.android.c2dm.intent.RECEIVE cmp=net.npike.android/gcm.GCMBroadcastReceiver (has extras) }
Broadcast completed: result=-1
... and your GCM BroadcastReceiver should fire up and build a notification (or whatever you have your GCM BroadcastReceiver doing.)
Step 6: Finish your testing and undo Step #2.
So depending on your environment it's kind of a pain-in-the-butt to test Google Cloud Messaging on an actual device.
Wouldn't it be nice if you could just test the code locally that would receive those magical GCM messages? *You can*.
adb shell is a magical place - a place where you can construct intents and fire them off into the system.
Step 1: Open your AndroidManifest.xml, locate your GCM Broadcast Receiver (let's say ours is net.npike.android.gcm.GCMBroadcastReceiver and our package name is net.npike.android)
<receiver
android:name="net.npike.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="net.npike.android"/>
</intent-filter>
</receiver>
Step 2: Temporarily remove the following attribute from its declaration:
android:permission="com.google.android.c2dm.permission.SEND"
Step 3: Open a terminal / command prompt where you can access ADB.
Step 4: Enter the shell.
adb shell
Step 5: Paste the following (replacing the appropriate values) and hit enter:
am broadcast -a com.google.android.c2dm.intent.RECEIVE --es "data.alert" "foo"
If you did everything correctly you should get some output like:
Broadcasting: Intent { act=com.google.android.c2dm.intent.RECEIVE cmp=net.npike.android/gcm.GCMBroadcastReceiver (has extras) }
Broadcast completed: result=-1
... and your GCM BroadcastReceiver should fire up and build a notification (or whatever you have your GCM BroadcastReceiver doing.)
Step 6: Finish your testing and undo Step #2.
相关文章推荐
- Android开源项目侧滑SlidingMenu的使用
- 利用Android的UXSS漏洞完成一次XSS攻击
- 最最实用的android studio设置教程
- Android与iOS之static关键字异同
- Android Navigation的使用
- Android之——JNI初探
- Android 开发笔记——通过 Intent 传递类对象
- Android开发案例:SQLite数据库和ListView列表结合
- Android测试二----InstrumentationTestRunner运行测试用例机制
- 快速掌握 Android Studio 中 Gradle 的使用方法
- 完美解决Android SDK Manager无法更新
- Android从SD卡加载大图裁剪为指定大小
- Android测试一----am启动应用流程
- android 声音抢焦点
- Ubuntu Kylin 14.04 64位系统安装Android Studio
- Android(java)学习笔记148:Eclipse中代码提示去掉@override,不然就报错!
- Android 如何获取view的宽高
- android view移动总结
- android meta-data的一个大坑--long类型数据
- Android JSON 解析库的使用 - Gson 和 fast-json