In a chat/voip application I needed to implement GCM in order to send notifications while device is sleep or my service is not running. Everything works fine until I swipe close the app or clear memory of my android device. After that my service is not starting and GCMListener is not getting fired anymore.
I have read almost all Stack overflow questions and answers regarding this issue but no solution found yet.
Simplified Android.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-sdk android:minSdkVersion="15" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.hardware.wifi"
android:required="true" />
<uses-feature
android:name="android.hardware.sip.voip"
android:required="false" />
<uses-feature
android:name="android.hardware.microphone"
android:required="true" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.example.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.app.permission.C2D_MESSAGE" />
<application
android:name="com.example.app.activity.MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:largeHeap="true"
android:logo="@drawable/logo"
android:theme="@style/AppTheme">
.... activity and other stuffs
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.app" />
</intent-filter>
</receiver>
<service
android:name="com.example.app.gcm.PushMessageReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.app.gcm.GCMInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.example.app.gcm.RegistrationIntentService"
android:exported="false">
</service>
</application>
</manifest>
GCMListenerService
public class PushMessageReceiver extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Intent intent = new Intent(this, MyService.class);
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.replaceExtras(data);
startService(intent);
}
}
}
I get following error in logcat -
05-11 14:47:59.278 23984-23984/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.example.app (has extras) }
I have actually two questions.
- Why my START_STICKY service is not getting restarted after a memory clean in device like miui and huawei.
- After cleaning memory or after couple of hour my GCMListener does not get fired anymore. But I am sure that I am getting GCM from server as I see that error on Logcat. How to make sure my GCMListener gets fire?
Any suggestion will be really helpful.
Thanks
Note: My app is receiving GCM if app in forground or background(pressed home). But not getting when swiped closed from recent app and clearing memory. I am not force closing my app. Just clearing RAM.