How to extract encapsulated JSON from Firebase Cloud Message? - android

I send from Firebase Messages in this structure:
{
"to": "/topics/myapp",
"data": {
"id" : "id123",
"title" : "lksdjkrfrfrereflsjd",
"body" : "fsdfsd",
"de": {
"key1" : "val1",
"key2" : "val2"
},
"en": {
"key3" : "val3",
"key4" : "val4"
}
}
}
in my override fun onMessageReceived(remoteMessage: RemoteMessage?) method my remoteMessage object has a data field which is from type: Map<String, String>
How can I access de and en from remoteMessage ?
EDIT: It is developed for Android

Related

How to filter list from data class

I have JSON response:
{
"response" : [
{
"f_name" : "иВан",
"l_name" : "ИваноВ",
"birthday" : "1987-03-23",
"avatr_url" : "https://2.cdn.echo.msk.ru/files/avatar2/2561900.jpg",
"specialty" : [{
"specialty_id" : 101,
"name" : "Менеджер"
}]
},
{
"f_name" : "Петр",
"l_name" : "петроВ",
"birthday" : null,
"avatr_url" : "https://2.cdn.echo.msk.ru/files/avatar2/1253126.jpg",
"specialty" : [{
"specialty_id" : 101,
"name" : "Менеджер"
}]
},
{"f_name" : "ЕКАТЕРИНА",
"l_name" : "пертрова",
"birthday" : "1990-01-07",
"avatr_url" : "",
"specialty" : [{
"specialty_id" : 102,
"name" : "Разработчик"
}]
"specialty" : [{
"specialty_id" : 102,
"name" : "Разработчик"
}]
and data class in Model:
data class Response(
#SerializedName("avatr_url")
val avatrUrl: String,
val birthday: String,
#SerializedName("f_name")
val fName: String,
#SerializedName("l_name")
val lName: String,
val specialty: List<Specialty>
)
data class Specialty(
val name: String,
#SerializedName("specialty_id")
val specialtyId: Int
)
It is required to filter the list of Response elements to be displayed in the RecyclerView for each profession. I load data for the entire list in Presenter:
val data = Employee.repository.getEmployeesInfo()
.response
How should I filter the list of items by the specialtyId property to form a list of people of only one profession?
the logic in this method should work:
fun filterBySpecialty(employees: List<Response>, specialtyId: Int): List<Response> =
employees.filter { employee ->
employee.specialty.any { specialty ->
specialtyId == specialty.specialtyId
}
}

How to read the payload of a Firebase Cloud Messaging notification?

I am using Firebase Cloud Messaging to send data notifications to other users. I'm sending a json similar to this:
{
"message":{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"type" : "MSG",
"id" : "abcdefg123456...."
}
}
}
And this code to receive notifications:
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
remoteMessage.notification?.let { notificacao ->
//How to read payload?
enviarNotificacao(notificacao.title ?: "", notificacao.body ?: "")
}
}
How do I read the data contained in "id" and "type"?
The id and type are both inside a data payload, to be able to read these values then try the following:
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "From: ${remoteMessage.from}")
// Check if message contains a data payload.
remoteMessage.data.isNotEmpty().let {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
}
// Check if message contains a notification payload.
remoteMessage.notification?.let {
Log.d(TAG, "Message Notification Body: ${it.body}")
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
remoteMessage.data will contain both the id and type
Check here for more info:
https://firebase.google.com/docs/cloud-messaging/android/receive
You can send data this way :
{
"to": "wOAT...",
"data": {
"title": "Notification title",
"message": "Notification message",
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
}
}
and fetch data this way :
MyFirebaseMessagingService
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(p0: RemoteMessage) {
val data = p0.data
val title = data["title"]
val message = data["message"]
val key1= data["value1"]
val key2= data["value2"]
val key3= data["value3"]
showNotification(title, message, ...)
}
}

How to send an array in dart post request

I try to send messages to device groups with their registration_ids.
This my code:
List<String> tokens=["token1","token2"];
final url='https://fcm.googleapis.com/fcm/send';
http.post(url,headers:{
"Accept": "application/json",
"Authorization":"key=mykey"
,"project_id":"proID"
},
body:
{
"registration_ids" :tokens ,
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
}
When the app runs this error displays:
Exception has occurred.
_CastError (type 'List' is not a subtype of type 'String' in type cast)
How to fix it?
Problem solved; I just encoded the body:
List<String> tokens=["token1","token2"];
final url='https://fcm.googleapis.com/fcm/send';
http.post(url,headers:{
"Accept": "application/json",
"Authorization":"key=mykey"
,"project_id":"proID"
},
body:json.encode(
{
"registration_ids" :tokens ,
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
)
}
you are having problems due to the registration is expecting a JSON string but you are passing list object to it. You can simply resolve that by casting your List<String> to String.
By casting the token list using toString() method you will get a String like this "['token1','token2']"
Here's the modified code:
List<String> tokens=["token1","token2"];
final url='https://fcm.googleapis.com/fcm/send';
http.post(url,headers:{
"Accept": "application/json",
"Authorization":"key=mykey"
,"project_id":"proID"
},
body:
{
"registration_ids" : tokens.toString() ,
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification"
}
}

Unable to use FirebaseRecyclerAdapter on case with list id

I have two fire object "chats" and "messages" like below. Each chat has list id of message.
"chats" : {
"13dc0865-616b-4963-bfdf-6c9b5d2011ef" : {
"buyer_id" : "dcf20b88-0612-4c13-933d-8b4f3d1838cb",
"created_at" : 1500676108229,
"item_id" : "fdab7447-e16d-4dac-8318-43f376ab3107",
"key1" : "fdab7447-e16d-4dac-8318-43f376ab3107_7060e0b7-420b-48fd-bc02-2e4868c6c2bb_dcf20b88-0612-4c13-933d-8b4f3d1838cb",
"key2" : "fdab7447-e16d-4dac-8318-43f376ab3107_dcf20b88-0612-4c13-933d-8b4f3d1838cb_7060e0b7-420b-48fd-bc02-2e4868c6c2bb",
"message_ids" : [ "-KpbG1tnCItaKykzEjaN", "-KpbG3cvMwitf_yzdYb6", "-KpbG9UhiOBJFR8GPE2x" ],
"seller_id" : "7060e0b7-420b-48fd-bc02-2e4868c6c2bb"
},
"25d667a3-6fe3-477d-b076-ac8d7b389133" : {
"buyer_id" : "dcf20b88-0612-4c13-933d-8b4f3d1838cb",
"created_at" : 1500674927785,
"item_id" : "b3985467-4e4e-4ecf-a914-48fb66fc5225",
"key1" : "b3985467-4e4e-4ecf-a914-48fb66fc5225_e68cf8f4-7470-45bf-8852-709d84fdae7f_dcf20b88-0612-4c13-933d-8b4f3d1838cb",
"key2" : "b3985467-4e4e-4ecf-a914-48fb66fc5225_dcf20b88-0612-4c13-933d-8b4f3d1838cb_e68cf8f4-7470-45bf-8852-709d84fdae7f",
"message_ids" : [ "-KpbBXhF5jjpspKgHPiV", "-KpbBpww9CcXL9n73J9j", "-KpbFgedT6RH2WZ6vjCV", "-KpbFxERv2adSRQHLnc2" ],
"seller_id" : "e68cf8f4-7470-45bf-8852-709d84fdae7f"
},
"messages" : {
"-KpbBXhF5jjpspKgHPiV" : {
"content" : "Tôi muốn mua Sua ban phim của bạn với giá 123.456 đ",
"created_at" : 1500674927777,
"sender_id" : "dcf20b88-0612-4c13-933d-8b4f3d1838cb"
},
"-KpbBpww9CcXL9n73J9j" : {
"content" : "Tôi muốn mua Sua ban phim của bạn với giá 123.456 đ",
"created_at" : 1500675006605,
"sender_id" : "dcf20b88-0612-4c13-933d-8b4f3d1838cb"
},
I am going to use FirebaseRecyclerAdapter to show messages for a chat conversation. My problem is on "chat" node, it only include the list message_id, the real data in inside node "messages".
How can I handle this case to work with FirebaseRecyclerAdapter ? (Sorry for my bad English)
From the chat node 13dc0865-616b-4963-bfdf-6c9b5d2011ef, you can access message_ids corresponding to it which are -KpbG1tnCItaKykzEjaN, -KpbG3cvMwitf_yzdYb6, -KpbG9UhiOBJFR8GPE2x.
Now you can get message data corresponding to the id as
(messages -> -KpbG1tnCItaKykzEjaN = your message). You can repeat this for remaining message ids.

Firebase Retrieve Limited Messages by Auto assigned timestamp

I wish to retrieve only the top 50 messages a user has within the database, with schema:
"individualMessages" : {
"99e4989b-a046-4c5f-9478-5ebd8bdc3ded" : {
"a96da7b1-7c4e-44bc-b82e-fc75bed52bcd" : {
"-KBUjngi9JStE-k4RMUJ" : {
"from" : "a96da7b1-7c4e-44bc-b82e-fc75bed52bcd",
"messageText" : "hey",
"timeSent" : "2016-02-26 17:40:46"
},
"-KBYhsRONfGxVI7Ysb3F" : {
"from" : "a96da7b1-7c4e-44bc-b82e-fc75bed52bcd",
"messageText" : "pool",
"timeSent" : "2016-02-27 12:10:52"
}
},
"fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de" : {
"-KBcYtQ5gD-drFZqz_MU" : {
"from" : "fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de",
"messageText" : "ouu",
"timeSent" : "2016-02-28 10:46:52"
},
"-KBcYw24M-9-7H5A3czD" : {
"from" : "fdb17f3a-7b7d-4aa5-9a0b-b9fb33c349de",
"messageText" : "gsf",
"timeSent" : "2016-02-28 10:47:03"
}
However, simply using:
individualMessageMyPath = myFeastFirebase.child("individualMessages").child(.....
getTop50Messages = individualMessageMyPath.limitToLast(5);
getTop50Messages.orderByChild("timeSent").addChildEventListener(mainMessagingListener);
Returns all messages on that node.
Reviewing the documentation this should work, however it does not.
What is missing?

Categories

Resources