marc_aragones
marc_aragones

Reputation: 4474

GCM push notifications not working if the app is closed in some devices

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?

Upvotes: 9

Views: 4192

Answers (2)

Naveen Kumawat
Naveen Kumawat

Reputation: 467

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).

Upvotes: 0

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

Upvotes: 1

Related Questions