0

I need to parse the notification body as a HashMap but the notification always displayed as a JSON string instead of the parsed message. Any ideas why?[Image of the Android notification received] (https://i.stack.imgur.com/IQIPb.jpg)

override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)
        Log.i("FirebaseService ", "Message :: $message")

        val title = message.notification!!.title
        val body = message.notification!!.body

        Log.i(TAG, "Messaging Service $body")

        val notificationBody: HashMap<String, String> = Gson().fromJson(body, HashMap::class.java) as HashMap<String, String>
        val messageBody = notificationBody["message"]
        val rideId = notificationBody["rideId"]

        if(notificationBody["type"] == "TRACK"){
            showNotification(title!!, messageBody!!)
        }else{
            // Something else
        }
    }

I have tried to parse the remote message using the GSON library, but the notification always return the full body

Liagi
  • 1
  • 1
  • If you're expecting something different to happen than what you're observing, then edit the question to explain what exactly that is and why. You should include your debugging information so that we can follow along with your code (since we aren't able to run it). – Doug Stevenson Jan 19 '23 at 20:05
  • see if [this](https://stackoverflow.com/a/42274768/14577209) answers your question. – Maleesha97 Jan 19 '23 at 20:47

1 Answers1

0

I finally found the answer on another stack overflow question:

Retrieve notification values from a notification in system tray android firebase FCM

The onMessageReceived function is not triggered in the background, the system tray handles the notification, so the notification data was not parsed. Now I need to handle the notification in the launcher event when the notification is triggered from the background.

Liagi
  • 1
  • 1