Android Question [solved] Local background notification / android 16

voxel

Active Member
Licensed User
Hello,
I'm trying to set up a local notification to be triggered in the background every 30 seconds. It works when the app is in the foreground, but when it's in the background, I get this error.
Thanks for your help.
Is there a link to some sample code? (I couldn't find it on the forum.)


Error : java.lang.RuntimeException: Unable to start service fr.lyzo.servicenotification@ae239d9 with Intent { cmp=fr.lyzo/.servicenotification (has extras) }: java.lang.RuntimeException: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{936f40 30919:fr.lyzo/u0a248} targetSDK=36

Manifest:
AddPermission(android.permission.POST_NOTIFICATIONS)
AddPermission(android.permission.WAKE_LOCK)
AddPermission(android.permission.SCHEDULE_EXACT_ALARM)
AddPermission(android.permission.USE_EXACT_ALARM)
AddPermission(android.permission.FOREGROUND_SERVICE)
AddPermission(android.permission.FOREGROUND_SERVICE_DATA_SYNC)
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)


Service:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Private n As Notification

End Sub

Sub Service_Create
   
    Dim n As Notification
   
    n.Initialize2(n.IMPORTANCE_LOW)

End Sub

Sub Service_Start (StartingIntent As Intent)
   
   
    Log("Service démarré")
   
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Titre de la notification", "Ceci est le texte de la notification.", Main)
    n.Notify(1) 'ID de notification
  
End Sub

Sub Service_Destroy
   
    Log("Service arrêté")

Main:
Sub Activity_Create(FirstTime As Boolean)
   
    If FirstTime Then
       
        'Pour éviter que le système tue votre service
        Dim in As Intent
        in.Initialize("android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS", _
        "package:" & Application.PackageName)
        Try
            StartActivity(in)
        Catch
            Log("Impossible d'ouvrir les paramètres d'optimisation batterie")
        End Try
   
        StartServiceAt(ServiceNotification, DateTime.Now + 30 * 1000, True)
   
    End If
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. targetSdkVersion should be 35 for now.

2. The OS will not let you start a service every 30 seconds. You should instead start the service when the app is in the foreground and keep it running "forever".

3. If you want to start a service while in the background then you should set the foreground type.
For example:
B4X:
SetServiceAttribute(Service2, android:foregroundServiceType, shortService)

And make sure to add:
B4X:
Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(NotificationId)
End Sub
 
Upvote 0

voxel

Active Member
Licensed User
Thank your for your answers.
My target is to launch a notification one a day
I will try as soon as i will be back from holidays :)
 
Upvote 0

voxel

Active Member
Licensed User
Hello,

I add this line in Manifest :

B4X:
SetServiceAttribute(ServiceNotification, android:foregroundServiceType, shortService)

It works

Thank
 
Upvote 0
Top