DRY Believer
DRY Believer

Reputation: 1018

How does GCM handle apps with BroadCastReceiver

I am having hard time understanding a basic concept as to how <intent-filter> handles BroadCastReceiver .Using below piece of code I have set a WakeFullBroadCastReciever which handles any downstream messages.My code runs fine and I am able to receive messages from GCM,but conceptually how is <receiver> able to recognize which app to open.No where in the flow diagram Google took my packageName inorder to match <category android:name>.Where am i conceptually going wrong and how is gcm handling this for two apps with gcm service on a device.

    <receiver
        android:name=".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.gcmexample" />
        </intent-filter>
    </receiver>  

Upvotes: 1

Views: 524

Answers (3)

Jeroen Mols
Jeroen Mols

Reputation: 3464

When your app registers itself to get its Push Notification token, you need to do something like this:

InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

Please look at a full example here: https://developers.google.com/instance-id/

The InstanceID is used here to get a unique token for every instance of your application. But it can also be used to do many other things, including verifying the packagename and signature of your application. (for more information, have a look here:https://developers.google.com/instance-id/)

So while I cannot be 100% sure (because it is closed source), Google probably passes along the packagename of your application when you register for your GCM token. So that one you receive a push notification, the Google GCM servers can simply send along the packagename along with the notification itself. The Google Play Services app on your device will then receive this notification and route it to the right application on your device.

Because of the InstanceID mechanism, this probably doesn't all happen explicitly, but at the end of the day this will be what happens.

Upvotes: 1

Dmide
Dmide

Reputation: 6462

I think both answers on this topic could answer your question.

In short: you should have supplied your application's package name and your release signing key's SHA-1 signature to Google console when you was obtaining API Key. The rest is done through Play Services.

Upvotes: 1

Barend
Barend

Reputation: 17419

It's a closed-source implementation, so I can't be sure, but I assume that the registration token that you use on the server side contains the required information for Google's systems to make the connection. In other words, it's either the GCM services in the cloud or the Play Services Client on the phone that correlates the registration token and its associated sender ID to the app's package name.

Upvotes: 2

Related Questions