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?

Malik Motani
  • 3,099
  • 3
  • 25
  • 49
muilpp
  • 1,123
  • 3
  • 14
  • 32

3 Answers3

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.

muilpp
  • 1,123
  • 3
  • 14
  • 32
  • 2
    useful information for everyone who targeting Huawei devices. Are all Huawei devices affected by this 'feature'? – ישו אוהב אותך Jun 25 '16 at 09:51
  • 1
    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. – muilpp Jun 25 '16 at 10:05
  • Some apps are enable in the protected apps by default. Any way we can mark our app as such? – SepehrM Oct 26 '16 at 12:05
  • @sepehrm According to the article provided in the answer and this [SO link](http://android.stackexchange.com/questions/152649/what-is-protected-apps-in-huawei-phones), it's not possible – muilpp Oct 26 '16 at 18:03
  • 1
    @muilpp 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? – SepehrM Oct 27 '16 at 10:41
  • @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](http://stackoverflow.com/questions/31638986/protected-apps-setting-on-huawei-phones-and-how-to-handle-it) where they talk about this. – muilpp Oct 27 '16 at 10:48
  • @SepehrM I am looking too for a way to automatically enable my app. Have you found a solution? – allemattio Feb 07 '17 at 15:02
  • @allemattio Unfortunately not – SepehrM Feb 11 '17 at 12:10
  • In my Huawei phone I was able to find this option in Settings>Apps>(Your app)>Battery and enable "Keep running after screen off" – Mark Pazon Jun 09 '17 at 07:06
  • Go to settings app choose your app then make sure you tick Show Notification and Untick Restrict to Launch – Nasz Njoka Sr. Jul 22 '17 at 15:46
0

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

Nasz Njoka Sr.
  • 1,086
  • 14
  • 26
  • 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 https://github.com/firebase/quickstart-android/issues/368 – sandeepd Jul 14 '20 at 10:19
-1

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

Chris K.
  • 248
  • 3
  • 9