Android Question Firebase topic life period

tzfpg

Active Member
Licensed User
Longtime User
I just want to confirm when i send notification using firebase topic. If the user offline or phone switch off, is it used can receive the notification after phone back to online?

I try send 10 notification to user when used phone offline. After user phone back to online, user didn't receive any notification.

How to make sure all the users can receive notification using firebase notification?
 

tzfpg

Active Member
Licensed User
Longtime User
I'm so noob, i read all the article but still don't no how to used the code.

B4X:
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;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub

Which part need to insert the code?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
This code is for sending the messages ( for not your main app ) , for a b4j app for example. Erel does not recommend it but you can try sending the message with firebase console and check with your app if you cant send msg with this code.
 
Upvote 0

tzfpg

Active Member
Licensed User
Longtime User
I know that code is not suitable used in B4A, now I like to move it to B4J write the server app to handle notification.
Now I like to confirm to send notification code is suitable for me.
I think I like to used delay_while_idle but don't know how to add to the send coding.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
The default "time_to_live" option is 4 weeks.

Expanding on the original question ... what is a devices topic subscription life ?
 
Upvote 0

tzfpg

Active Member
Licensed User
Longtime User
B4X:
 Dim fcmkey As String = "xxxxxxxx"
Dim _3 As String = "{""delay_while_idle"":true,"
Dim _5 As String = """data"": {"
Dim _4 As String = """Time:"":""" & datetime_now & ""","
Dim _6 As String = """message"": """ & msg & """},"

Dim new_json As String = _3 & _5 & _4 & _6 & """registration_ids""" & ": [" & fcmkey & "]}"

is it this format are correct?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
It is much better to send a message to a device: (ID)

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

With "data" you can send a map like you want. Very flexible. Add the "delay" when you want.

I have a db where I have a Status column. Here I store if the message was received an when.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why is it better to send to a device instead to a topic?
I actually think that it is better to use topics. This way you don't need to run an online server to collect the token ids.

You can send the same information to both topics and ids.

Expanding on the original question ... what is a devices topic subscription life ?
It is not documented so I cannot say.
You should call Subscribe each time that the Starter service is created.

I know that code is not suitable used in B4A, now I like to move it to B4J write the server app to handle notification.
The code is suitable to B4A as well. It is a simple http request made with OkHttpUtils2.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…