-->

GCM Broadcast Receivers

2019-08-07 09:07发布

问题:

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