如果应用程序在某些设备中关闭,则GCM推送通知无效

mar*_*nes 9 android google-cloud-messaging

我已经按照Google的GCM 设置指南和他们在Github上的示例创建了一个接收通知的应用程序.

它什么时候起作用:

  • 应用程序打开(所有设备)
  • 应用程序在后台(所有设备)
  • 应用已关闭(除一个外)

手机无法正常工作:

  • 华为P8精简版
  • 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>
Run Code Online (Sandbox Code Playgroud)

在项目级别添加了此依赖项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'
  }
}
Run Code Online (Sandbox Code Playgroud)

在应用级别添加了此依赖项build.gradle:

compile "com.google.android.gms:play-services-gcm:8.1.0"
Run Code Online (Sandbox Code Playgroud)

Java类与Github示例相同.

当用户关闭应用程序时,为什么此设备无法正常工作,但它适用于我测试过的所有其他设备?

小智 1

这是华为设备的一个“错误/功能”。

Android - GCM - 未在后台接收推送通知

一旦您杀死了小米、华为这些手机上的应用程序,就会注销该应用程序的广播接收器和服务。

如果收到通知,您的 GCM 广播接收器会在终止 >app 时未注册,这可能就是原因。

用户必须将您的应用程序添加到电池管理器的时间列表中

https://www.forbes.com/sites/bensin/2016/07/04/push-notifications-not-coming-through-to-your-huawei-phone-heres-how-to-fix-it/#578f42bd1ccc

在 Android 5.0.1 的华为 Gra-L09 上测试

编辑:

您可以警告用户并启动受保护的应用程序管理器。

华为手机“受保护的应用程序”设置及处理方法