Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM Intent service OnRegistered is not getting called

I have created a GCM Intent service and the device is getting registered successfully. I also receive the REGISTRATION Intent, but the OnMessage or OnRegistered methods are not getting called.

Below the log i see.

07-23 06:24:23.542: V/GCMBroadcastReceiver(1168): onReceive: com.google.android.c2dm.intent.REGISTRATION
07-23 06:24:23.542: V/GCMBroadcastReceiver(1168): GCM IntentService class: com.app.demo.myApp.GCMIntentService
07-23 06:24:23.581: V/GCMBaseIntentService(1168): Acquiring wakelock

Below is the code for OnMessage.

Could someone pls help me as to why the OnRegistered or OnMessage is not getting called.

 public class GCMIntentService extends GCMBaseIntentService {



    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "registered", Toast.LENGTH_LONG).show();
        String device_id = GCMRegistrar.getRegistrationId(this);
        GSLogger.Log("mess_received", device_id);
    }

    @Override
    protected void onRegistered(Context context, String arg1) {
        Toast.makeText(context, "registered", Toast.LENGTH_LONG).show();
        String device_id = GCMRegistrar.getRegistrationId(this);
        GSLogger.Log("registered", device_id);

    }

}

Manifest Code:

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-configuration android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE"/>


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
     <permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" 
        android:protectionLevel="signature" />
    <uses-permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" />

Receiver Code in Manifest:

<receiver
            android:name="com.google.android.gcm.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.app.demo.myApp" />
          </intent-filter>
        </receiver>
        <service android:name="com.app.demo.myApp.GCMIntentService" />

Pic of My Package: enter image description here

like image 471
Raghav Avatar asked Jul 23 '13 06:07

Raghav


3 Answers

The issue is

<service android:name="com.app.demo.myApp.GCMIntentService" />

Just give

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

And place your GCMIntentService Java file in the folder as specified by your package name in manifest.

For example if you have declared com.app.demo.myApp as the application package name

Place your GCMIntentService in the same folder structure.

like image 54
Nargis Avatar answered Sep 30 '22 11:09

Nargis


I see some potential problems with your manifest, though I'm not sure whether they are the cause to your problems :

The following is not required :

<uses-permission android:name="com.google.android.c2dm.permission.C2D_MESSAGE"/>

You already have the correct C2D_MESSAGE permission :

 <permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="com.app.demo.myApp.permission.C2D_MESSAGE" />

In addition, GCM is supported from API level 8, so you probably shouldn't specify android:minSdkVersion="7"

like image 20
Eran Avatar answered Sep 30 '22 11:09

Eran


Make sure you put your GCMIntentService in the main folder..don't put it under any sub folder, and it will work. I had the same issue and it started working.

like image 24
nithinreddy Avatar answered Sep 30 '22 10:09

nithinreddy