I am stucked in integrating GCM push notification to android app. I obtained the device id and the server side seems to be ok, as it gives success message. I think the issue is with the android part. Please check my codes. My manifest file is provided below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.purpletab.sanghi">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<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" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<permission android:name="com.purpletab.sanghi.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.purpletab.sanghi.permission.C2D_MESSAGE" />
<application
android:name=".BaseApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:largeHeap="true"
android:theme="@style/AppTheme"
>
<activity
android:name=".activities.SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.DashActivity"
android:label=""
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"/>
<activity
android:name=".activities.CoreActivity"
android:label=""
android:parentActivityName=".activities.DashActivity"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.purpletab.sanghi.activities.DashActivity" />
</activity>
<activity android:name=".activities.SignInActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
/>
<activity android:name=".activities.OtpActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"></activity>
</application>
<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" />
<category android:name="com.purpletab.sanghi" />
</intent-filter>
</receiver>
<service android:name="com.purpletab.sanghi.GcmMessageHandler" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</manifest>
GCMBroadcastReceiver file is provided below
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
GcmBroadcastReceiver()
{
System.out.println("RECEIVER IS WORKING...");
}
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
System.out.println("RECEIVED NOTIFICATION............");
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GCMMessageHandler Class is given below
public class GcmMessageHandler extends IntentService implements Observer, AppConstants {
String mes;
private Handler handler;
//private PushReceivingTemplate details;
private BaseApp baseApp;
public GcmMessageHandler() {
super("GcmMessageHandler");
System.out.println("HANDLER IS WORKING...");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
baseApp = (BaseApp) getApplication();
baseApp.getObserver().addObserver(this);
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("message");
/*
Gson gson = new Gson();
Type collectionType = new TypeToken<PushReceivingTemplate>() {
}.getType();
details = gson.fromJson(mes, collectionType);
final DatabaseHandler dbh = new DatabaseHandler(this);
dbh.addNotidfications(details);
//mes = getResources().getString(R.string.gcmPush);
if(baseApp.getObserver().isAppIsInBackground(this))
{
NotificationUtils.displayNotification(this, details, baseApp);
}
//}*/
System.out.println("____________________" + mes);
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showToast(){
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void update(Observable observable, Object data) {
}
}
From the server I receive this message
{"multicast_id":6519739741263216481,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1457419626958900%8c03edcaf9fd7ecd"}]}
Please solve the issue.