0

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.

  1. Why my START_STICKY service is not getting restarted after a memory clean in device like miui and huawei.
  2. 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.

  • do you get a callback inside your onMessageReceived method when swiped form recent app list? – Gautam May 11 '17 at 09:13
  • @Gautam No. If I just press home button, then I get that callback fired. – Shimul Chowdhury May 11 '17 at 09:18
  • See my answer [here](http://stackoverflow.com/a/39505298/4625829) – AL. May 12 '17 at 05:09
  • @Al Yeah thats what is happening as I am testing my app. However I observed logcat of imo. And I see they seems to using SyncAdapter to run their service back on. In your answer you told about it too. Did you managed to find the way how others doing it? (if that is one possible solution) – Shimul Chowdhury May 12 '17 at 05:43
  • @AL also I am noticing that my AlarmManager and JobScheduler both gets cleared once RAM is cleared. I do want to respect battery uses and user experience on my app. But to make my app functioning I am not seeing other ways. – Shimul Chowdhury May 12 '17 at 05:45
  • @AL One more thing to add, I have already tried to give autostart permission on Xiaomi. It does not help much. My app also have battery optimization whitelist. That doesnot helping either. On Xiaomi (After all permission) my app works great for couple of hours and then it gets killed and never wakes back until user tap on it. – Shimul Chowdhury May 12 '17 at 05:48

1 Answers1

1

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

Fernando Tan
  • 634
  • 4
  • 14
  • How apps like whatsapp and viber gets that permission by default? Is there any Api from manufacturers to ask for permission from User? – Shimul Chowdhury May 11 '17 at 09:22
  • It is weird but famous apps like whatsapp will get the permission by default. I was spending few days to troubleshoot this. AFAIK, there is no API to let the application ask for permission, since it is very specific to certain manufacturers – Fernando Tan May 11 '17 at 09:24