I've followed Google's GCM setup guide and their example on Github to create an app that receives notifications.
When does it work:
The phone that is not working:
This phone works fine, it can receive WhatsApp messages or any other kind even if the user kills the app.
I am afraid this could happen in many other devices I haven't test. So I want to show off some code in order to see if there are any problems.
Here my 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>
Added this dependency in the project-level 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'
}
}
Added this dependency in the app-level build.gradle
:
compile "com.google.android.gms:play-services-gcm:8.1.0"
And the Java classes are the same of the Github example.
Why is this device not working when the user closes the app but it works in all other devices I have tested?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
The basics of push notifications A push notification is delivered instantly to a mobile device, regardless of whether the device is locked or unlocked. The push notification is delivered even if the user is in a different app or away from the app sending the push notification.
Settings > Sounds & Vibration > Do Not Disturb: if this setting is enabled, Push Notifications will not be received. Make sure this is disabled. Settings > General > Background App Refresh: this setting allows the app to run in the background and must be turned on.
Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.
It's a "bug/feature" of Huawei devices.
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.
User has to add your app on a whilelist on the battery manager
https://www.forbes.com/sites/bensin/2016/07/04/push-notifications-not-coming-through-to-your-huawei-phone-heres-how-to-fix-it/#578f42bd1ccc
Tested on Huawei Gra-L09 with Android 5.0.1
EDITED:
You can warn the user and launch Protected Apps manager.
"Protected Apps" setting on Huawei phones, and how to handle it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With