gpt4 book ai didi

android - 如果应用程序在某些设备中关闭,GCM 推送通知将不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:20 25 4
gpt4 key购买 nike

我关注了 Google 的 GCM setup guide和他们的 example on Github创建一个接收通知的应用程序。

它什么时候起作用:

  • 打开应用(所有设备)
  • 后台应用(所有设备)
  • 应用已关闭(除了一个)

打不通的电话:

  • 华为P8 lite
  • 安卓 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 example 相同.

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

最佳答案

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

Android - GCM - Not receiving push notifications on background

Once you kill application on xiaomi, huawei these phones unregister broadcast receivers, services of the application.

In case of notification, your GCM broadcastreceiver get unregistered on killing >app, that is likely the reason for this.

用户必须将您的应用添加到电池管理器的白名单中

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 上测试

已编辑:

您可以警告用户并启动 protected 应用程序管理器。

"Protected Apps" setting on Huawei phones, and how to handle it

关于android - 如果应用程序在某些设备中关闭,GCM 推送通知将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33697368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com