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?