某些设备中,如果应用程序关闭,则GCM推送通知无法正常工作

9
我按照Google的GCM设置指南和他们在Github上的示例创建了一个接收通知的应用程序。
它什么时候工作:
- 应用程序已打开(所有设备) - 应用在后台(所有设备) - 应用程序关闭(除一个之外的所有设备)
不工作的手机:
- 华为P8 lite - Android 5.1 - Google Play服务8.3.01
这部手机很好用,即使用户杀死应用程序,它也可以接收WhatsApp消息或任何其他类型的消息。
我担心这可能会发生在许多其他我没有测试的设备上。因此,我想展示一些代码,以查看是否存在任何问题。
这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.presentation" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.example.presentation.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.presentation.permission.C2D_MESSAGE" />

    <application
        android:name=".AndroidApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <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.presentation" />
                <!-- If you want to support pre-4.4 KitKat devices. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            </intent-filter>
        </receiver>
        <service android:name="com.example.data.gcm.RegistrationIntentService"
            android:exported="false" />
        <service
            android:name="com.example.data.gcm.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name="com.example.data.gcm.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>

    </application>

</manifest>

在项目级别的build.gradle中添加了这个依赖项:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    classpath 'com.google.gms:google-services:1.4.0-beta3'
  }
}

在应用级别的build.gradle中添加了这个依赖项:

compile "com.google.android.gms:play-services-gcm:8.1.0"

Java类与Github示例相同。

为什么当用户关闭应用时,此设备无法工作,但在我测试的所有其他设备上都可以工作?


我认为由于手机操作系统的更改,如果您关闭应用程序,则它会真正关闭,而其他设备并不会真正关闭(onDestroyed)应用程序,而是让其在后台运行。如果您想要接收消息,可以尝试将优先级设置为高。 - bjiang
@bjiang 你是指通知的优先级吗?问题在于应该构建通知的服务从未启动。 - marc_aragones
@marc_aragones 我也遇到了同样的问题。我进一步调查了这个问题,发现 GCM 推送实际上已经到达设备,但是 Google Play 服务在事件中显示为“已取消”。我的应用程序从未收到接收推送的回调。要获取 Google Play 服务的日志,请拨打 #426#。这适用于除三星设备外的所有设备。如果您已经找到解决方案,请告诉我。 - Naeem
遇到了与我小米设备相同的问题 :( - RAJESH KUMAR ARUMUGAM
2个回答

1
这是华为设备的“bug/特性”。 Android - GCM - Not receiving push notifications on background 在小米、华为等手机上,一旦你杀死应用程序,该应用程序的广播接收器和服务将被注销。 在通知的情况下,您的GCM广播接收器在杀死应用程序时会被注销,这可能是原因。
用户必须将您的应用程序添加到电池管理器的白名单中。
在华为Gra-L09上测试,使用Android 5.0.1。
编辑:您可以警告用户并启动受保护的应用程序管理器。
“Protected Apps”设置在华为手机上的操作方法请参见https://www.forbes.com/sites/bensin/2016/07/04/push-notifications-not-coming-through-to-your-huawei-phone-heres-how-to-fix-it/#578f42bd1ccc

0

现在,小米、乐视和一些新的定制操作系统为了安全目的禁用了每个应用程序的通知。按照以下步骤,在使用清理工具退出应用程序后,仍然可以在后台接收消息。

设置 -> 权限(只需在此处添加您的应用程序即可)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接