Android Question FirebaseNotifications data null

gerredtor

Active Member
Licensed User
hello i send from firebase a notification, and the data return null:

Message data: {}

my code(from the tut):

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
   Log($"Message: ${Message}"$)
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
   n.Notify(1)
End Sub
 

gerredtor

Active Member
Licensed User
look my picture.

i will use this, it only works for me when i fill key and value.. but this is a very inefficient way for supporters and others..

and my code is from the tut for testing:

B4X:
Starter:

Sub Service_Create
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
End Sub

FirebaseMessaging:

Sub Process_Globals
   Private fm As FirebaseMessaging
End Sub

Sub Service_Create
   fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
   fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub Service_Start (StartingIntent As Intent)
   If fm.HandleIntent(StartingIntent) Then Return
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
   Log("Message arrived")
   Log($"Message data: ${Message.GetData}"$)
  
   If Message.GetData == Null Or Message.GetData.Get("title") == Null Then
       Return
   End If
  
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
   n.Notify(1)
End Sub

Sub Service_Destroy

End Sub
 

Attachments

  • Unbenannt.PNG
    56 KB · Views: 385
Upvote 0

erdi

Member
Licensed User
Longtime User
I've played with the tutorial software a bit and it works as long as the program is active as a service. The moment I activate the program and leave the main program active the first message succeeds but the following messages all return data "null". All help is appreciated.
 

Attachments

  • PagerTest.zip
    183.2 KB · Views: 243
Upvote 0

KMatle

Expert
Licensed User
Longtime User
See here (my explanations at the end): https://www.b4x.com/android/forum/t...rebase-opener-notification.72642/#post-462424

As you see the "notification" is in real a message which has two parts: the notification AND the data (or only one of them). Google handles the notification automatically for you when the app is in the background Only when the user clicks on it, the app starts again (like WhatsApp).

B4X:
'{
'    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
'    "notification" : {
'      "body" : "great match!",
'      "title" : "Portugal vs. Denmark",
'      "icon" : "myicon"
'    },
'    "data" : {
'      "Nick" : "Mario",
'      "Room" : "PortugalVSDenmark"
'    }
'  }
  
  
   Dim m As Map = CreateMap("to": $"${Devtoken}"$)
   Dim noti As Map = CreateMap("body":"great match!","title":"Portugal vs. Denmark","icon": "myicon")
   Dim data As Map = CreateMap("Nick":"Mario","Room":"PortugalVSDenmark")
   m.Put("notification", noti)
   m.Put("data", data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.Tag=jg
   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)
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
I'm having the same problem, except I am trying to send a notification from Google Firebase console. I fill in the Message Text and also the Title but the app returns null.
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
ME TOO; The problem is in receiving msg! If activity hidden/in background i receive notification title/body, but when activity in foreground i got null for both title & body!!!
Any suggestions ...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…