Sub Process_Globals
Private fm As FirebaseMessaging
Dim Title As String
Dim Body As String
Dim PostLinks As List ' List to store post links
End Sub
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
If FirstTime Then
fm.Initialize("fm")
End If
fm.HandleIntent(StartingIntent)
End Sub
Sub FetchPostLinks
Dim apiUrl As String = "https://yeseuropa.org/wp-json/wp/v2/posts"
Dim job As HttpJob
job.Initialize("fetchPosts", Me)
job.Download(apiUrl)
End Sub
Sub JobDone(job As HttpJob)
If job.Success Then
Dim response As String = job.GetString
Dim parser As JSONParser
parser.Initialize(response)
Dim posts As List = parser.NextArray
PostLinks.Initialize ' Initialize the list to store post links
For Each post As Map In posts
Dim postLink As String = post.Get("link")
PostLinks.Add(postLink)
Next
Else
Log("HTTP request failed: " & job.ErrorMessage)
End If
job.Release
End Sub
Public Sub SubscribeToTopics (Topics() As Object)
For Each topic As String In Topics
fm.SubscribeToTopic(topic)
Next
End Sub
Sub fm_MessageArrived (Message As RemoteMessage)
Log("Message arrived")
Log($"Message data: ${Message.GetData}"$)
' Get title and body, providing defaults if they are missing or null
Dim Title As String = Message.GetData.GetDefault("title", "YES EUROPA MOVILIDAD INTERNACIONAL")
Dim Body As String = Message.GetData.GetDefault("body", "Aquí te enviamos una nueva oportunidad publicadas en YesEuropa.")
Dim PostLink As String = Message.GetData.Get("post_url")
Dim NewsId As Int = Message.GetData.Get("news_id")
If PostLink = Null Then
Log("No post link found in the notification data.")
Return
End If
Log($"Message data: Title=${Title}, Body=${Body}, PostLink=${PostLink}, NewsId=${NewsId}"$)
FetchPostLinks
If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
' App is in the foreground
Dim n2 As Notification
n2.Initialize2(n2.IMPORTANCE_HIGH)
n2.Icon = "icon"
n2.SetInfo(Title, Body, Main)
n2.Notify(1)
' Open the post link in the phone's browser when the notification is clicked
Dim i As Intent
i.Initialize(i.ACTION_VIEW, PostLink)
StartActivity(i)
End If
' Handle image notification logic here from the first code snippet
' Be sure to adjust paths and variables as needed
Try
Log("Message arrived")
Log($"Message data: ${Message.GetData}"$)
Dim imageloader As BitmapsAsync
imageloader.Initialize
Dim n As NB6
n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(False).SmallIcon(Application.Icon)
If Message.GetData.Get("image") <> "" Then
Dim j As HttpJob
j.Initialize("", Me)
j.Download(Message.GetData.Get("image"))
Wait For (j) JobDone (j As HttpJob)
If j.Success Then
Wait For (imageloader.LoadFromHttpJob(j, 500dip, 500dip)) Complete (bmp As B4XBitmap)
Dim b As Bitmap = bmp
End If
n.BigPictureStyle(LoadBitmap(File.DirAssets,"icon.png"),b,Message.GetData.Get("title"),Message.GetData.Get("body"))
n.Build(Message.GetData.Get("title"),Message.GetData.Get("body"),"noti", Main).Notify(2000)
Else if Message.GetData.Get("image") = "" Then
n.Build(Message.GetData.Get("title"),Message.GetData.Get("body"),"noti", Main).Notify(2000)
End If
Catch
n.Build(Message.GetData.Get("title"),Message.GetData.Get("body"),"noti", Main).Notify(2000)
Log(LastException)
End Try
End Sub
Sub fm_TokenRefresh (Token As String)
Log("TokenRefresh: " & Token)
'Create a http job object
Dim job As HttpJob
job.Initialize("job1", Me)
'Subscribe device
Dim params As Map
params.Initialize
Dim r As Reflector
r.Target = r.GetContext
Dim secureSettings As Object = r.RunMethod("getContentResolver")
Dim jo As JavaObject
Dim device_id As String = jo.InitializeStatic("android.provider.Settings$Secure").RunMethod("getString", Array As Object(secureSettings, "android_id"))
params.Put("rest_api_key", "xxxxxxxxxxxxxxxx")
params.Put("device_uuid", device_id) 'Use the device uid from dgUID library
params.Put("device_token", Token) 'Use the refreshed token
params.Put("subscription", "your_subscription")
params.Put("device_name", "your_device_name")
params.Put("os_version", "your_os_version")
'Post the parameters to the subscribe endpoint
job.PostMultipart("https://www.yeseuropa.org/wp-json/fcm/pn/subscribe", params, Null)
'Wait for the job to complete
Wait For (job) JobDone(job As HttpJob)
'Check the result
If job.Success Then
'Parse the JSON response
Dim parser As JSONParser
parser.Initialize(job.GetString)
Dim root As Map = parser.NextObject
'Get the error, message and subscription_id fields
Dim hasError As Boolean = root.Get("error")
Dim message2 As String = root.Get("message")
Dim subscription_id As Int = root.Get("subscription_id")
If hasError Then
' Handle the error
Log(message2)
Log("Subscription id: " & subscription_id)
Else
Log("Error: " & job.ErrorMessage)
End If
Else
' Handle the error
Log(job.ErrorMessage)
End If
' Release the job object
job.Release
End Sub
Public Sub UnsubscribeFromTopics (Topics() As Object)
Dim r As Reflector
r.Target = r.GetContext
Dim secureSettings As Object = r.RunMethod("getContentResolver")
Dim jo As JavaObject
Dim device_id As String = jo.InitializeStatic("android.provider.Settings$Secure").RunMethod("getString", Array As Object(secureSettings, "android_id"))
' Create a http job object
Dim job As HttpJob
job.Initialize("job2", Me)
' Unsubscribe device
Dim params2 As Map
params2.Initialize
params2.Put("rest_api_key", "xxxxxxxxxxxxxxxxxx")
params2.Put("device_uuid", device_id)
'Post the parameters to the unsubscribe endpoint
job.PostMultipart("https://www.yeseuropa.org/wp-json/fcm/pn/unsubscribe", params2, Null)
'Wait for the job to complete
Wait For (job) JobDone(job As HttpJob)
'Check the result
If job.Success Then
'Parse the JSON response
Dim parser2 As JSONParser
parser2.Initialize(job.GetString)
Dim root2 As Map = parser2.NextObject
'Get the error and message fields
Dim hasError As Boolean = root2.Get("error")
Dim message2 As String = root2.Get("message")
Dim subscription_id As Int = root2.Get("subscription_id")
If hasError Then
' Handle the error
Log(message2)
Log("Subscription id: " & subscription_id)
Else
Log("Error: " & job.ErrorMessage)
End If
Else
' Handle the error
Log(job.ErrorMessage)
End If
' Release the job object
job.Release
End Sub