Shimul Chowdhury
Shimul Chowdhury

Reputation: 191

Android GCM not received when RAM cleared

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.

Upvotes: 0

Views: 223

Answers (1)

Fernando Tan
Fernando Tan

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

Related Questions