tony9099
tony9099

Reputation: 4727

GCM clarifications

I know these methods are deprecated, but since the new GCM API seems to be buggy, I am reverting to these methods until a stable version is pushed by Google.

We are declaring this receiver inside the manifest.

        <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.myApp" />
        </intent-filter>
    </receiver>

  <service android:name=".GCMIntentService" />

And we have the onMessage() method inside the GCMIntentService class.

   @Override
protected void onMessage(Context context, Intent intent) 
{
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("msg");
}

1. However, upon receiving a message this method is never called. Why ?

Moreover, the example I follow uses the following.

registerReceiver(mHandleMessageReceiver, new IntentFilter("intent_filter_string"));

associated with the following class.

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() 
   {
    @Override
    public void onReceive(Context context, Intent intent) 
   {
        String newMessage = intent.getExtras().getString("data");
    }
 };

which gets unregistered inside the onPause.

  1. Why do we need to create this Broadcast Receiver?
  2. Can't we do this in the manifest ?
  3. Isn't this already covered by the onMessage() inside the GCMIntentService class ?
  4. What role does the Pending Intent String play ?

Answers are appreciated.

Upvotes: 0

Views: 136

Answers (2)

Sunil Mishra
Sunil Mishra

Reputation: 3826

Why do we need to create this Broadcast Receiver?

In some cases you might be interested in updating the UI if the app is running.
So you create a broadcast receiver at runtime and unregister it when the app
goes into background.

Can't we do this in the manifest?

Yes, you can do it in manifest too.

Isn't this already covered by the onMessage() inside the GCMIntentService class?

GCMIntentService extends GCMBaseIntentService. So any message coming from gcm,
will first be recieved in the onMessage of the GCMIntentService.
Its upto you to decide how you handle the message.
You can create a notification or send a broadcast to your custom broadcast
receivers and update the UI when the app is running.

What role does the Pending Intent play ?

According to the docs

A PendingIntent itself is simply a reference to a token maintained by the system 
describing the original data used to retrieve it. This means that, even if its
owning application's process is killed, the PendingIntent itself will remain
usable from other processes that have been given it.

Upvotes: 1

Alex Hoffmann
Alex Hoffmann

Reputation: 31

Did you registered your app/device combination to your GCm project? You have to do that first in the onRegistered Method. And did you add all necessary permissions? Google says you dont have to add the Custom Permissions above android 4.0 but my apps never worked without'em. If you're looking for an easier way to work with GCM I recommend apiOmat: http://www.apiomat.com I know its Backend as a Service. But if your app is small you don't have to pay anything and it's much easier with it.

Upvotes: 0

Related Questions