Android Question How to close FireBase opener notification

Quintus

New Member
Licensed User
Hello,

I configured a FireBase Cloud Messaging to receive push notifications. All works perfect and the device receives messages.

When my app runs (on foreground) the "MessageArrived" event let me read the message, initialize a notification etc. but, when my app runs in background or is closed, the message is placed on the notification list by Android system without a "MessageArrived" event in my app.

In this case, when I click on the notification my program starts correctly but, how can I read the message text and close the opener notification from the "Activity_Resume" ?

I use B4A 6.30.

Many thanks for any help.

Regards.
 

Quintus

New Member
Licensed User
No. Following firebase.google guides I wrote a HTTP POST request on my IIS server. I send (as json) only "data" and "title".
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
the message is placed on the notification list by Android system without a "MessageArrived" event in my app

Could you post the parms of the request? It seems to depend on what message type you send. Via console I can reproduce that behaviour (but not with my app) Will check the data structure....
 
Last edited:
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Found it: https://firebase.google.com/docs/cloud-messaging/android/receive

Handle notification messages in a backgrounded app
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). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

So this means that a message has two parts: The notification itsself AND/OR the data. If you just send a "notification" without data, it is just a notification. If your app is in the foreground, it will receive it. If not, it's just a notification and will be displayed by Android.

If it HAS data, the data will be delivered in the extras of the intent of your launcher Activity.

Clicking on your notification (without data) will start your app but there is no data to fetch.

This was new for me, too. I really thougt a notification is a MESSAGE containing some data and it was the developers job to throw a "real" notification. But it is really an automatic notification. That explains why there is an "icon" and "sound" parameter in the console.

Data messages:

B4X:
Data messages

Set the data key with your custom key-value pairs to send a data payload to the client app. Data messages can have a 4KB maximum payload.

For example, here is a JSON-formatted message in the same IM app as above, where the information is encapsulated in the data key and the client app is expected to interpret the content:

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
}

NON-data (just a notification):

B4X:
{
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    }
  }

Both:

B4X:
{
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Quintus

New Member
Licensed User
Great KMatle !

I modified my HTTP POST request JSON with, as first section, the "notification" and its values (body, title, sound etc.) and then the "data" section with my custom-key values.

Now when I click a notification received with the app in background, my app starts, the notification auto-close it self and in the intent.Extras there are all my "data" custom values !


Many thanks, KMatle and Erel, for your prompt help.

Bye.
 
Upvote 0
Top