#Region Module Attributes
'#StartAtBoot: False
#End Region
'Service module
'Version 0.9
Sub Process_Globals
'Dim sNotif As Notification
Private fm As FirebaseMessaging
End Sub
Sub Service_Create
fm.Initialize("fm")
End Sub
Public Sub SubscribeToTopics
fm.SubscribeToTopic("ios_general")
fm.SubscribeToTopic("alarm1")
End Sub
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
Sleep(0)
Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub
'Sub fm_MessageArrived (Message As RemoteMessage)
' Log("Message arrived")
' Log($"Message data: ${Message.GetData}"$)
' Dim n As Notification
' n.Initialize
' n.Icon = "icon"
' n.Sound = True
' n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
'
' CallSubDelayed2(Main, "MessageFromFirebase",Message.GetData.Get("body") & " " & Message.GetData.Get("title"))
'
' n.Notify(1)
'End Sub
Sub fm_MessageArrived (Message As RemoteMessage)
Log("Message arrived")
Log($"Message data: ${Message.GetData}"$)
'n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
Dim rp As RuntimePermissions
Dim folder As String = rp.GetSafeDirDefaultExternal("shared")
Dim FileName As String = "shotgun.mp3"
'copy the file to the shared folder
File.Copy(File.DirAssets, FileName, folder, FileName)
Dim n As NB6
n.Initialize("custom sound", Application.LabelName, "HIGH")
n.SmallIcon(LoadBitmapResize(File.DirAssets, "logo_trans.png", 32dip, 32dip, True))
'disable the default sound
n.SetDefaults(False, True, True)
'set custom sound
n.CustomSound(CreateFileProviderUri(folder, FileName))
Dim Notification As Notification = n.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), "", Main)
Notification.Notify(9)
CallSubDelayed2(Main, "MessageFromFirebase",Message.GetData.Get("body") & " " & Message.GetData.Get("title"))
End Sub
Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
Dim FileProvider As JavaObject
Dim context As JavaObject
context.InitializeContext
FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
Dim f As JavaObject
f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub