Android Question Firebase notifications (key/value)

aidymp

Well-Known Member
Licensed User
Longtime User
Can we use the key / value part of the firebase notifications? if so how would you implement this?

2016-09-09_19-43-46.png


I believe that if you put something like START and PAGE2 and some code to receive the key/value anyone opening the notification the app would open on page2 (that's just an example) can anyone shed any light on this?

Thanks

Aidy
 

KMatle

Expert
Licensed User
Longtime User
See here: https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/


To send a message to a single device (id = token):


B4X:
Private Sub SendMessage(id As String, Title As String, Body As String)
   Dim Job As HttpJob
   Job.Initialize("fcm", Me)
   Dim m As Map = CreateMap("to": $"${id}"$)
   Dim data As Map = CreateMap("title": Title, "body": Body)
   m.Put("data": data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
See here: https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/


To send a message to a single device (id = token):


B4X:
Private Sub SendMessage(id As String, Title As String, Body As String)
   Dim Job As HttpJob
   Job.Initialize("fcm", Me)
   Dim m As Map = CreateMap("to": $"${id}"$)
   Dim data As Map = CreateMap("title": Title, "body": Body)
   m.Put("data": data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub

Hi, thanks for that, so its not for what I initially thought??
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Hi, thanks for that, so its not for what I initially thought??

It's very easy. Just follow the steps in Erel's example.

Just debug what the device receives :) Just send a message and see...

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
  
   Dim Data As String = Message.GetData.Get("message")  
   
End Sub

Your are free which key/value pairs you use:

B4X:
Dim Job As HttpJob
   Job.Initialize("Loc", Me)
   Dim m As Map = CreateMap("to": $"${devtoken}"$)
   Dim data As Map = CreateMap("nick": Nick, "loc": loc)
   m.Put("data", data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)

Here I use "nick" and "loc". As I've written: Just try... (send & debug)
 
Upvote 0
Top