How to run GcmListenerService in the foreground

I sometimes have a problem with my GCM service, which closes when the RAM of my smartphone is automatically cleared (read this if you need more information).

As I understand it, if I installed my service to work in the foreground, it should help the system remove it using RAM. The Service.class method onStartCommand() usually used to start the startForeground() method.

But with the latest version of the GCM implementation, this is not possible, since the onStartCommand() method of the parent GCMListenerService.class is defined as final , and I cannot override it.

So, how can I install the gcm receiver on the rum in the foreground?

Here is my manifest part about GCM.

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.app.path" > <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- ... other permissions --> <permission android:name="my.app.path.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="my.app.path.permission.C2D_MESSAGE" /> <application ...> <!-- ... activites... --> <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="my.app.path" /> </intent-filter> </receiver> <service android:name=".MyGcmListener" android:exported="false" android:enabled="true" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service> </application> </manifest> 

Thanks in advance.

+8
android google-cloud-messaging
source share
1 answer

This question is quite old, but I think it is worth answering for the benefit of future readers.

With GCM (or the new FCM) it is NOT NECESSARY to keep the GCMListenerService in the foreground.
In fact, there is no need to support the application at all.

When the GCM message is received by the system, your application will be launched and the corresponding service (GCMListenerService in GCM or FirebaseMessagingService in FCM) will be executed.

The real issue raised by this entry:
GCM messages do not arrive when the application is down.

This is due to the xiaomi feature that blocks GCM messages.
see "Auto whitelist" in the "Security" section.
http://en.miui.com/thread-117992-1-1.html

0
source share

All Articles