GCM Broadcast Receiver onReciever is not called

10,711

Solution 1

Here is how you can use Google's cloud messaging service using the gcm.jar library that google provides.

Go to SDK Manager>Extras and download "Google Cloud Messaging for android" library. You can find the jar file in android installation folder. Android>extras>google>gcm>gcm-client>dist>gcm.jar

Include this jar in your project.

Step 1: modify your manifest file:

add this receiver

    <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" />
        </intent-filter>
    </receiver>

add this service:

<service android:name="com.your.package.GCMIntentService" />

Step 2: Create GCMIntentService extending GCMBaseIntentService. Make sure you put it in your app's default package as defined in the manifest's package attribute in manifest.xml

this is how it looks

public class GCMIntentService extends GCMBaseIntentService {

@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onMessage(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onRegistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

}

Step 3: Register a device.

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
GCMRegistrar.register(this, SENDER_ID);

this SENDER_ID is the sender ID (or project ID i forgot) you got while registering for GCM. That's it you are done. Now override any method in GCMIntentService the way you want. May be log some information and see. The overriden methods have obvious names. Like onRegistered will be called when device registration is complete the String argument is the GCM registration ID. for more info click here

hope it helps.

Solution 2

I would suggest you remove current projects and start afresh. Follow steps in this tutorial carefully for implementation.

GCM standard documentation is quite confusing. I followed the above tutorial and successfully implemented GCM.

Additional notes:

  1. GCM requires an emulator with Google APIs.
  2. For android versions less than 4.2, there must be at least one logged-in google account.
Share:
10,711
Mj1992
Author by

Mj1992

Updated on July 24, 2022

Comments

  • Mj1992
    Mj1992 over 1 year

    I am trying to implement notifications for my android application but I am not getting OnReceive event of MyBroadcastReciever being called.

    Here's my Manifest File:

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <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.example.gcm.permission.C2D_MESSAGE" 
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gcmtester.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <receiver
            android:name="com.example.gcm.MyBroadcastReceiver"
            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.example.gcm" />
            </intent-filter>
        </receiver>
        <service android:name="com.example.gcm.MyIntentService" />
    </application>
    </manifest>
    

    Here's how I am trying to call the service.

        Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
        registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        registrationIntent.putExtra("sender", "MY_SENDER_ID");
        startService(registrationIntent);
    

    The MyIntentService code is also similar to the one mentioned here. But still my BroadcastReciever onRecieve method is not being called. What is the problem am I forgetting something ?

    Note: MyIntentService and BroadcastReciever classes are in a seperate package(com.example.gcm) from the Main Activity package.

  • drulabs
    drulabs about 11 years
    you don't need a GCMBroadcastReceiver... It is internally implemented in gcm.jar. The receiver starts GCMIntentService.
  • Mj1992
    Mj1992 about 11 years
    I ran into some errors but somehow managed to solve them. Now the problem is that I am not able to get the registerationID for the device. can you please tell me how can I get that.I placed a log in the intentService OnRegistered Event to get the regId but the debugger is not reaching there.
  • Mj1992
    Mj1992 about 11 years
    thanks alot for your help. I will surely follow the tutorial to make things more clear.
  • Renjith
    Renjith about 11 years
    If you find the answer useful, don't forget to place your vote.
  • drulabs
    drulabs over 9 years
    Category field is not required, I just updated the answer. Thanks for pointing that out.
  • Emil Reña Enriquez
    Emil Reña Enriquez about 9 years
    jut note: this tut is deprecated