GCM Broadcast Receiver onReciever is not called
android, google-cloud-messaging
Solution
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.
Problem
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.