muilpp
muilpp

Reputation: 1343

Firebase cloud messaging not received when app in background

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?

Upvotes: 7

Views: 10254

Answers (3)

Nasz Njoka Sr.
Nasz Njoka Sr.

Reputation: 1118

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

Upvotes: 0

muilpp
muilpp

Reputation: 1343

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.

Upvotes: 15

Chris K.
Chris K.

Reputation: 258

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

Upvotes: -1

Related Questions