MetalCraneo
MetalCraneo

Reputation: 335

Android GCMIntentService onMessage context issue

I'm trying to implement GCM to my app in Android. The server and client side setup seems to be right, because the onMessage method is called when I "push" a string from the server side. I can read the extras from the intent, however, using a notification or a Toast message doesn't work. The phone doesn't show anything, even with the app running, so I guess something isn't right with the context object I use from the callback. Here's the revelvant part of the manifest.xml, where PACKAGE is the base package.

<receiver
   android:name="PACKAGE.gcm.GCMReceiver"
   android:enabled="true"
   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="PACKAGE" />
     </intent-filter>
</receiver>

<service
   android:name="PACKAGE.gcm.GCMIntentService"
   android:enabled="true" />
</application>

<permission
    android:name="PACKAGE.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="PACKAGE.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>

Now the onMessage Method:

@Override
    protected void onMessage(Context context, Intent intent)
    {
        if (DEBUG)
        {
            System.out.println("[GCMIntentService] Message Received! " + intent.getStringExtra("message"));
        }

        Toast.makeText(context, intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        // int icon = R.drawable.notification_icon;
        CharSequence tickerText = intent.getStringExtra("message");
        long when = System.currentTimeMillis();

        Notification notification = new Notification(0, tickerText, when);

        CharSequence contentTitle = "Some Notification";
        CharSequence contentText = tickerText + " some more text";

        notification.setLatestEventInfo(context, contentTitle, contentText, null);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);

    }

The Toast and the Notification aren't working. I call the service and registration routines in the onCreate of the first Activity, wich is used as Splash and closed after a few seconds. Might that have something to do with it?

Upvotes: 2

Views: 5957

Answers (1)

Chris.Jenkins
Chris.Jenkins

Reputation: 13129

This isn't a Context issue.

GCM reciever runs in an intent service which in its self runs on a separate thread.

To show a toast just call from inside a UIThread. Which you can do like so:

Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable(){

    Toast.makeText(context, intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();

});

This will now post on your UI thread and you will then see the toast!

(You will need to make your local variable's final for them to be executed by the anonymous class)

Cheers!

Upvotes: 6

Related Questions