Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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?

like image 628
marc_aragones Avatar asked Nov 13 '15 16:11

marc_aragones


People also ask

How do I get notifications if an app is closed?

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.

Does an app need to be open for push notifications?

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.

Why are my push notifications not working?

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.

Do push notifications work when app is closed iOS?

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.


1 Answers

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

like image 167
Ivan Sorribes Montanyés Avatar answered Oct 19 '22 02:10

Ivan Sorribes Montanyés