简体   繁体   English

如何在前台运行GcmListenerService

[英]how to run GcmListenerService in foreground

I have a problem sometimes with my GCM service which is closed when RAM of my smartphone is automatically cleared (read this if you need more details). 我的GCM服务有时会出现问题,当我的智能手机的RAM自动清除时,该服务会关闭(如果需要更多详细信息,请阅读信息)。

As far as I understand if I set my service to run in foreground it should help the system to to delete it with RAM. 据我了解,如果我将服务设置为在前台运行,它应该可以帮助系统使用RAM将其删除。 The method of Service.class onStartCommand() is usually used to run startForeground() method. Service.class onStartCommand()方法通常用于运行startForeground()方法。

But with the latest version of GCM implementation it is impossible since the onStartCommand() method of the parent GCMListenerService.class is defined as final and I cannot override it. 但是使用最新版本的GCM实现是不可能的,因为父GCMListenerService.classonStartCommand()方法定义为final,而我无法覆盖它。

So how can I set my gcm receiver to rum in foreground? 那么如何设置我的gcm接收器在前台朗姆酒呢?

Here is my manifest part about GCM. 这是我关于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>

Thank you in advance. 先感谢您。

This question is quite old but I think it's worth to answer for the benefit os the future readers. 这个问题已经很老了,但是我认为有必要为将来的读者带来好处。

With GCM (or the new FCM) there is NO NEED to keep GCMListenerService in foreground. 使用GCM(或新的FCM)时无需将GCMListenerService保持在前台。
Indeed there is no need to keep the app running at all. 实际上,根本不需要保持应用程序运行。

When a GCM message is received by the system, your app will be started and the relevant service (GCMListenerService in GCM or FirebaseMessagingService in FCM) will be executed. 系统收到GCM消息后,将启动您的应用,并执行相关服务(GCM中的GCMListenerService或FCM中的FirebaseMessagingService)。

The real issue raised by this post is: 这篇文章提出的真正问题是:
GCM messages are not arriving when the application is not running. 当应用程序未运行时,GCM消息未到达。

This is due to a special feature of xiaomi that blocks GCM messages. 这是由于xiaomi的一项特殊功能阻止了GCM消息。
see: Autostart whitelist inside the Security section. 请参阅:“安全性”部分内的自动启动白名单。
http://en.miui.com/thread-117992-1-1.html http://en.miui.com/thread-117992-1-1.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM