如何在前台运行GcmListenerService [英] how to run GcmListenerService in foreground

查看:100
本文介绍了如何在前台运行GcmListenerService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的GCM服务有时会出现问题,当我的智能手机的RAM被自动清除时,该服务会关闭(请阅读 )。



据我了解,如果我将服务设置为在前台运行,它应该帮助系统用RAM删除它。通常使用 Service.class onStartCommand()方法运行 startForeground()方法。

但是随着最新版本的GCM实现,它是不可能的,因为 onStartCommand()父节点的方法 GCMListenerService.class 被定义为 final ,我无法覆盖它。



那么我怎样才能在前台设置我的gcm接收器到朗姆酒?



这是我关于GCM的部分清单。

 <?xml version =1.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =my.app.path>

<使用权限android:name =android.permission.INTERNET/>
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>

<使用权限android:name =android.permission.WAKE_LOCK/>
< uses-permission android:name =android.permission.VIBRATE/>
< uses-permission android:name =com.google.android.c2dm.permission.RECEIVE/>
<! - - 其他权限 - >

< permission
android:name =my.app.path.permission.C2D_MESSAGE
android:protectionLevel =signature/>

<使用权限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>
< category android:name =my.app.path/>
< / intent-filter>
< / receiver>

< service
android:name =。MyGcmListener
android:exported =false
android:enabled =true>
< intent-filter>
< / intent-filter>
< / service>

< / application>

< / manifest>

预先感谢您。

解决方案

这个问题相当古老,但我认为值得为未来的读者带来好处。



使用GCM(或新的FCM),没有必要将GCMListenerService保留在前台。

确实没有必要保持应用程序运行。



系统接收到GCM消息时,您的应用程序将启动,相关服务(GCM中的GCMListenerService或FCM中的FirebaseMessagingService)将被执行。



这篇文章提出的真正问题是:

GCM消息在应用程序未运行时未到达。



这是由于xiaomi的一项特殊功能会阻止GCM消息。

请参阅:安全部分中的自动启动白名单。

http://en.miui.com/thread-117992-1 -1.html


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).

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. The method of Service.class onStartCommand() is usually used to run startForeground() method.

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.

So how can I set my gcm receiver to rum in 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>

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.
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.

The real issue raised by this post is:
GCM messages are not arriving when the application is not running.

This is due to a special feature of xiaomi that blocks GCM messages.
see: Autostart whitelist inside the Security section.
http://en.miui.com/thread-117992-1-1.html

这篇关于如何在前台运行GcmListenerService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆