Project

General

Profile

FCM Push notifications for Android

Vlad Alexandru
Added 11 months ago

Hello!

While testing the Android app and after looking through the code, I didn't really understand how the push component works.
From what I understand, when the app is closed the XMPPService is kept alive, so messages are not received through push component. If the XMMPService is shut down, no notification will be received, maybe because the server is implemented to send FCM messages only with data, which are not sent to the Android notification tray. The only way i could test the push component was when the app was in foreground, and user presence was OFFLINE, but in the onMessageReceived() method of MyFirebaseMessagingService nothing happens if the user is OFFLINE.

The way I would have seen this working was to send from server to FCM a message with this structure:
{

"notification": {
    "title": "<sender nickname or jabberId>",
    "text": "<the content of message>"
},

"data": {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
},

"to": "< device token >"

}

This way, by having the "notification" component in the structure, a notification would be shown to the user about receiving a message even if the app is completely shut down, and maybe the XMPPService should be started only if the user taps on that notification.

Please tell me if I am wrong, and explain to me the way it was thought to work.


Replies (1)

Added by Andrzej Wójcik IoT 1 CloudTigaseTeam 10 months ago

Push notifications in case of Android XMPP client were designed to be delivered when the account is enabled as in another case if you disable an account then you are not interested in any messages or notifications about them. Due to that, we filter notification on the client side to make sure that account is enabled.

We are also not using notification key in even, as we do not want to display a notification with JID of the recipient but we want to display contact name as set in the user's roster. We cannot access users roster on the server side inside push component as XEP-0357 do not mention user jid to be sent as a part of the payload to the push component which may be located on the different server that XMPP server and may not have access to the roster item details.

Due to that, we decided that we will be sending push notifications when XMPP client is disconnected but XMPP service is active as it is required to display a notification in the proper form. This means that it works only when XMPP client is disconnected (ie. due to firewall restrictions or network issues), while XMPP service is active in the background and still trying to reconnect. We decided to do so, because:

  • usage of FCM to deliver notifications is not guaranteed to work (delivery if possible)
  • message is delivered in plaintext to the client (readable to FCM servers)

We prefer to (if only possible) to deliver messages using XMPP connection directly to XMPP client without relying on FCM and due to that we use it to wake up application only if XMPP connection is not working but XMPPService is up (not manually killed by the user).

    (1-1/1)