Reputation: 191
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.
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.
Upvotes: 0
Views: 223
Reputation: 644
Some of the phone manufacturers e.g. XiaoMi and Huawei disallow the application from running in the background unless user allow it. It means background service will be killed when user swipe off or clean the RAM
For XiaoMi, user can grant the access to app by adding it to AutoStart list.
For Huawei, it´s called TelephonyManager. There are a view options where you have to whitelist your app
Upvotes: 1