I recently updated my app to send messages through FCM and it works great when the app is run on Jelly Bean. The problem is it doesn't in case it's Lollipop and the app is in the background.
I read the documentation and it's stated that when the payload is a data message, it will be handled by onMessageReceived(). I am sending a data payload, not a notification, and it's handled correctly as long as it's JellyBean. For Lollipop it only handles the message if the app is in the foreground. If not, nothing happens.
This is how the beginning of my FirebaseMessagingService looks like:
public class FirebaseBroadcastReceiverService extends FirebaseMessagingService {
private final String TAG = FirebaseBroadcastReceiverService.class.getName();
@Override
public void onMessageReceived(RemoteMessage message){
Log.i(TAG, "A message is received");
String from = message.getFrom();
Map<String, String> data = message.getData();
.....
}
}
The manifest:
<application
android:name=".controller.application.AppResources"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_shadow"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Black.NoTitleBar">
<service
android:name=".model.firebase.FirebaseListenerService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".model.firebase.FirebaseBroadcastReceiverService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
.......
</application>
And the payload:
{
"data": {
"test": "test"
},
"to" : "MY FCM REGISTRATION ID"
}
Which is sent via POST to https://fcm.googleapis.com/fcm/send with the Authorization and Content-type headers.
I might have read all other SO threads related to this but I couldn't find an answer to it and there are also no log traces in the Android Monitor. Does anyone have a hint on this?
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).
There are two types of messages in FCM (Firebase Cloud Messaging): Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground. Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed.
Ok, turns out Huawei has some power-saving features which cause that some apps won't be able to receive notifications, so it's not related to the Android version.
I couldn't find a way to fix this programmatically, but if anyone's interested, go to Settings / Protected apps and select the apps you want to allow to be run on the background, so they receive notifications.
More info here.
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