Android Question Receive Firebase Notifications while deepsleep [SOLVED]

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I need to receive Notifications 24/7 even when App is killed or the phone is in deepsleep. I use the following as Receiver:
B4X:
Sub Process_Globals
    Public fm As FirebaseMessaging
    Public xKeer As Int
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
        fm_TokenRefresh("")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

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

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message: "&Message.GetData)
    SetAlarm
    Notify("Er is een notifcation binnen")
End Sub

Sub fm_TokenRefresh(Token As String)
    Wait For (Starter.GetAdvertisingId) Complete (Id As String)
    If Id <> "" Then
        Starter.ParameterVast=Starter.ParameterVast.Replace("&deviceid="&EigenFuncties.GetDeviceId,"&deviceid="&Id)
        'Log(Starter.ParameterVast)
        EigenFuncties.DeviceID=Id
    End If
    Log("DeviceID: "&Id)
    Log("EigenID: "&EigenFuncties.DeviceID)
    Log("fmID: "&fm.Token)
    Starter.fmToken=fm.Token
    CallSub(Starter,"RegistreerToken")
End Sub

Sub SetAlarm
    Dim i As Intent
    Dim Begin As Long
    Dim H, M, S As Int

    Begin = DateTime.Now

    H = DateTime.GetHour(Begin)
    M = DateTime.GetMinute(Begin)
    S = DateTime.GetSecond(Begin)
        
    i.Initialize("android.intent.action.SET_ALARM", "")
       i.PutExtra("android.intent.extra.alarm.HOUR", H)
       i.PutExtra("android.intent.extra.alarm.MINUTES", M+1)
    i.PutExtra("android.intent.extra.alarm.MESSAGE", "CIRIS Strooi")
       i.PutExtra("android.intent.extra.alarm.SKIP_UI", True)
      StartActivity(i)
End Sub

Sub Notify(Body As String)
    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("strooi", Application.LabelName, "HIGH")
    n.SmallIcon(LoadBitmapResize(File.DirAssets, "icon.png", 32dip, 32dip, True))
    'disable the default sound
    n.SetDefaults(False, True, True)
    'set custom sound
    n.CustomSound(CreateFileProviderUri(folder, FileName))
    n.BadgeIconType("SMALL")
    n.Number(6)
    Dim Notification As Notification = n.Build("CIRIS Strooi", Body, "CIRIS Strooi", Oproep)
    Notification.Notify(299)
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

While the App is in Foreground, it works fine.
While the App is in Background, Notification is received, but no Sound and a different Icon
While the App is Killed or Phone is Sleeping, no action at all.

How can I prevent this from happening? I read something about a WakefulBroadcastReceiver in a recently reply like:
B4X:
class BackgroundFcmReceiver : WakefulBroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
    val extras = intent.extras?.keySet()
    if (extras != null) {
        for (key in extras) {
            Log.d("TAG", "$key")
        }
    }
}

Is this something what I can do with B4A?

Kind regards,
André
 

OliverA

Expert
Licensed User
Longtime User
 
Upvote 0
Top