Android : GCM intent services OnRegistered method is not getting called

on Wednesday, August 6, 2014


In the log I am getting this message.


V/GCMBroadcastReceiver(30668): onReceive: com.google.android.c2dm.intent.REGISTRATION


08-07 11:12:59.096: V/GCMBroadcastReceiver(30668): GCM IntentService class: com.example.notifications.GCMIntentService


08-07 11:12:59.096: V/GCMBaseIntentService(30668): Acquiring wakelock


But OnRegistered method is not been invoked ( Running app in my MotoG device ). Below is the manifest.xml coding.



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notifications"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<permission android:name="com.example.notifications.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.notifications.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 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" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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.example.notifications" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>


And my MainActivitiy.java file



public static final String SENDER_ID = "xxxxxxxxx";
GCMRegistrar.checkDevice(MainActivity.this);
GCMRegistrar.checkManifest(MainActivity.this);
ChannelUri = GCMRegistrar.getRegistrationId(getApplicationContext());
if(ChannelUri.equals(""))
{
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}
else
{
Toast.makeText(getApplicationContext(), "Already Registered", Toast.LENGTH_SHORT).show();
}


GCMIntentService.java



public class GCMIntentService extends GCMBaseIntentService {

public static String sRegistrationId;

protected GCMIntentService() {
super(MainActivity.SENDER_ID);
}


public static String getRegistrationId()
{
return sRegistrationId;
}



@Override
protected void onError(Context arg0, String arg1) {
Log.e("Registration", "Got an error!");
Log.e("Registration", arg0.toString() + arg1.toString());

}

@Override
protected void onMessage(Context arg0, Intent intent) {

Log.i("Registration", "Got a message!");
Log.i("Registration", arg0.toString() + " " + intent.toString());


}

@Override
protected void onRegistered(Context arg0, String arg1) {
sRegistrationId = arg1;
Log.i("Registration", "Just registered!");
Log.i("Registration", arg0.toString() + arg1.toString());
}

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

}
}


Project Structure Src -> com.example.notifications -> GCMIntentService.java, MainActivity.java


0 comments:

Post a Comment