'Handle RemoteMessage object using data class in Kotlin
I am using firebase in order to receive push notification from the server. The push notification I receive from the server is in Json format. For example:
{
"data": {
"body": "This is test notification message!",
"date": "2021-04-01 08:50",
"title": "test"
},
"registration_ids": [
"some fcm token"
]
}
I would like to parse this object using data class Here is my class:
data class ServerRemoteMessage (
@SerializedName(DATA) val data : Data
)
data class Data (
@SerializedName(TITLE) val title : String,
@SerializedName(BODY) val body : String
)
Here is the way I'm trying to handle it, without success:
class PushMessagingService : FirebaseMessagingService() {
val TAG: String
get() = "PushMessagingService"
override fun onMessageReceived(remoteMessage: RemoteMessage) {
handleRemoteMessageData(remoteMessage)
}
override fun onNewToken(token: String) {
Log.d(TAG, "token $token")
// TODO send token to the serve
}
private fun handleRemoteMessageData(remoteMessage: RemoteMessage) {
val remoteMessage = Gson().fromJson(Gson().toJson(remoteMessage.data), ServerRemoteMessage::class.java)
val title = remoteMessage.data.title
val body = remoteMessage.data.body
NotificationUtils.showNotification(this, title, body,
R.drawable.ic_notification, R.color.blue,
getString(R.string.app_name))
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
