9

I've followed Google's GCM setup guide and their example on Github to create an app that receives notifications.

When does it work:

  • App opened (all devices)
  • App in background (all devices)
  • App closed (all except one)

The phone that is not working:

  • Huawei P8 lite
  • Android 5.1
  • Google Play Services 8.3.01

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?

marc_aragones
  • 4,344
  • 4
  • 26
  • 38
  • I think due to the OS changed on the phone that if you closes the app it real closed while other devices is not real close(`onDestroyed`) the app, but let it running on the background. If you wanna receive the message, you can try to set priority to high. – bjiang Nov 16 '15 at 18:12
  • @bjiang Do you mean notification's priority? The problem is that the service which should build the notification is never started. – marc_aragones Nov 19 '15 at 23:59
  • @marc_aragones I am facing same issue. I have further investigated the issue and found that GCM Push actually lands on device but Google Play services shows in events that its CANCELED. My Application never gets callback for received push. For getting logs of Google Play services you dail *#*#426#*#*. This will work on all devices except Samsung devices. Please let me know if you have found the solution. – Naeem Jan 19 '16 at 11:47
  • faced the same issue with my Xiomi device :( – RAJESH KUMAR ARUMUGAM Mar 26 '17 at 07:55

2 Answers2

1

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

Community
  • 1
  • 1
0

Now a days, Xiaomi and LeEcho and some new custom OS for security purposes disables the Notification for each Application. Follow these steps to receive messages in background once they quit the app using cleaner.

Settings -> Permissions (Just add your app here and done).

Naveen Kumawat
  • 467
  • 5
  • 12