GCM Broadcast Receivers

六月ゝ 毕业季﹏ 提交于 2019-12-11 06:39:23

问题


So I've been working with Pubnub and GCM. Although this question is more GCM/Android specific than Pubnub. I've 2 apps - one is my own the other is pubnub's example.

Pubnub manifest's receiver tag:

<receiver
    android:name="com.pubnub.pubnubexample.GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.example.gcm" />
    </intent-filter>
</receiver>

My app's receiver tag:

<receiver
    android:name="com.myapp.app.MyReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.example.gcm" />
    </intent-filter>
</receiver>

Apparently the pubnub sample app's broadcast receiver does gets called but not the one in mine. Both of them have these uses-permission:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Infact mine has even more.

What might I be doing wrong that my own app's receiver isn't being called by GCM when a notification is sent through it ? My expectation was that since it's a broadcast multiple apps with similar receivers would receive them ?

Note: This is not really a requirement in my work but testing and trying to understand how this concept works.


回答1:


One issue I can see is that the category tag in your AndroidManifest file must be updated to your package name.

<category android:name="com.myapp.app." />



回答2:


When your app registers with GCM it uses the a sender_id and GCM returns a registration_id. I believe the registration_id would be unique for each app. You can check this by examining the registration_id that is returned when each app resisters with GCM. If you want the message to be delivered to both apps, then you would need to send it to both registration_ids



来源:https://stackoverflow.com/questions/27316455/gcm-broadcast-receivers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!