Android Question Firebase push notification

uniplan

Active Member
Licensed User
Longtime User
Hi, i followed this example to sent a notification push.

https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/

I'm not included the last part where you can send the message to topic.
I'm interested only on the part where you can sent the message from firebase to all devices.

I test it and i receive the notification push, but the title and body are null.
you can help me about this problem?
Thank you for your help
 

KMatle

Expert
Licensed User
Longtime User
The firebase console is just a little helper and not ment to be to send messages generally. Use The B4J example to send the messages.

To the null values: Check the content of "Message.GetData" (just log it and see what is sent)
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
Firstly i used the firebase console and i receive the push notification, but it's null.
Now i'm testing the B4J example but receive this message in console:

Waiting for debugger to connect...
Program started.
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
[jobname=fcm, success=false, username=
, password=, errormessage=Unauthorized, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@70177ecd, tag=java.lang.Object@1e80bfe8
, httputils2service=null]
Program terminated (StartMessageLoop was not called).
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
The firebase console is just a little helper and not ment to be to send messages generally. Use The B4J example to send the messages.

To the null values: Check the content of "Message.GetData" (just log it and see what is sent)
Message.GetData is empty....
in console:
Message data: {}
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
I follow correctly this steps...only part that i not added is this:

'************ Firebase Ads ************AddApplicationText(
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity android:name="com.google.android.gms.ads.purchase.InAppPurchaseActivity"
android:theme="@style/Theme.IAPTheme"/>
)'************ Firebase Ads (end) ************

Becouse i receive this error
AndroidManifest.xml:155: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.IAPTheme').
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
Now i added the FirebaseAdMob library and i haven't this error message...but already push is null
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
The strange behaviour is the B4J program...that display this message in console:
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
[jobname=fcm, success=false, username=
, password=, errormessage=Unauthorized, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@70177ecd, tag=java.lang.Object@1e80bfe8
, httputils2service=null]
Program terminated (StartMessageLoop was not called).
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region

Sub Process_Globals
Private const API_KEY As String = "my_API_KEY"
End Sub

Sub AppStart (Args() As String)
SendMessage("general", "Benvenuto", "Hello!!!!")
SendMessageToSingleDevice("my_device_id","hi")
StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
Dim Job As HttpJob
Job.Initialize("fcm", Me)
Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
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")
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub


Private Sub SendMessageToSingleDevice(Devtoken As String, datastring As String)

Dim Job As HttpJob
Job.Initialize("SendMessage", Me)

Dim m As Map = CreateMap("to": $"${Devtoken}"$)
Dim noti As Map = CreateMap("body":"Notification ", "title":"New message!")
Dim data As Map = CreateMap("data": datastring)
m.Put("notification", noti)
m.Put("data", data)
m.Put("content_available": True)
Dim jg As JSONGenerator
jg.Initialize(m)
Job.Tag=jg
Log(jg.ToString)
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


Sub JobDone(job As HttpJob)
Log(job)
If job.Success Then
Log(job.GetString)
End If
job.Release
StopMessageLoop '<-- non ui app only
End Sub
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
This is the response in console:

Waiting for debugger to connect...
Program started.
{"notification":{"body":"Notification ","title":"New message!"},"content_available":true,"data":{"data":"hi"},"to":"MY_DEVICE_ID"}
[jobname=SendMessage, success=true, username=
, password=, errormessage=, target=class b4j.example.main
, taskid=1, req=anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest@3d494fbf, tag=anywheresoftware.b4j.objects.collections.JSONParser$JSONGenerator@1ddc4ec2
, httputils2service=null]
{"multicast_id":8955244386403551542,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1489412586732815%f4d36675f4d36675"}]}
Program terminated (StartMessageLoop was not called).
 
Upvote 0
Top