Firebase cloud messaging not received when app in background
Asked Answered
D

3

7

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?

Dukedom answered 24/6, 2016 at 14:47 Comment(3)
and what does your fcm payload look likeNoncompliance
Tim, I added it to the questionDukedom
Check this answer! https://mcmap.net/q/73224/-how-to-handle-notification-when-app-in-background-in-firebaseClaritaclarity
D
15

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.

Dukedom answered 24/6, 2016 at 17:10 Comment(10)
useful information for everyone who targeting Huawei devices. Are all Huawei devices affected by this 'feature'?Dittography
in the link I provided they talk about Mate 8, Mate S and Honor, mine is a P8 lite and it happens too, but i'm not sure if this is the behaviour for all Huawei devices.Dukedom
Some apps are enable in the protected apps by default. Any way we can mark our app as such?Fibrosis
@sepehrm According to the article provided in the answer and this SO link, it's not possibleDukedom
@Dukedom So why does Huawei enable Telegram, WhatsApp and alike by default? I really don't think there is a "white list" that includes popular and trusted apps. Where does that list come from and how does it get updated?Fibrosis
@sepehrm It looks like there is. Like I said above, I couldn't find a way to do this programatically, but if you find it you'll be more than welcome to share it with us! Here's another question where they talk about this.Dukedom
@Fibrosis I am looking too for a way to automatically enable my app. Have you found a solution?Harm
@Harm Unfortunately notFibrosis
In my Huawei phone I was able to find this option in Settings>Apps>(Your app)>Battery and enable "Keep running after screen off"Griskin
Go to settings app choose your app then make sure you tick Show Notification and Untick Restrict to LaunchCultus
C
0

Go to settings app choose your app then make sure you tick Show Notification and Untick Restrict to Launch

Cultus answered 22/7, 2017 at 15:45 Comment(1)
it looks its dependent on vendor implementation. for Lenovo Vibe K5 Plus, I see this, and by unticking restrict-to-launch, it delivers the message even when app was swiped-up from recents. go through this thread github.com/firebase/quickstart-android/issues/368Immorality
E
-1

It sounds like you're missing wake-lock permission in your Manifest file

Etana answered 24/6, 2016 at 15:1 Comment(3)
Don't think that is the problemNoncompliance
firebase does not require that permission to work ... developers.google.com/cloud-messaging/android/…Cultus
definitely the Wake-Lock permission is not the problemHistogenesis

© 2022 - 2024 — McMap. All rights reserved.