Android Question Service.StartForeground(1, Notif1) under Android 13

peacemaker

Expert
Licensed User
Longtime User
HI, All

How to use a foreground service under Android13, if notifications are not required ?
Locally working app with timers and screen.

B4X:
Sub Service_Create
Notif1.Initialize2(Notif1.IMPORTANCE_LOW)
Service.StartForeground(1, Notif1)

Notif1 is required for StartForeground, but
java.lang.ClassCastException: anywheresoftware.b4a.objects.NotificationWrapper$NotificationData cannot be cast to android.app.Notification
 

peacemaker

Expert
Licensed User
Longtime User
full stack trace from the logs.
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
*** Service (serv) Create ***
Error occurred on line: 25 (serv)
java.lang.IllegalArgumentException: method anywheresoftware.b4a.objects.ServiceHelper.StartForeground argument 2 has type android.app.Notification, got anywheresoftware.b4a.objects.NotificationWrapper$NotificationData
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at peacemaker.randomsignal.serv.onCreate(serv.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4688)
at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2248)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:211)
at android.os.Looper.loop(Looper.java:300)
at android.app.ActivityThread.main(ActivityThread.java:8296)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:559)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:954)
 
Upvote 0

Rasoull

Member
HI, All

How to use a foreground service under Android13, if notifications are not required ?
Locally working app with timers and screen.

B4X:
Sub Service_Create
Notif1.Initialize2(Notif1.IMPORTANCE_LOW)
Service.StartForeground(1, Notif1)

Notif1 is required for StartForeground, but
I have the same problem, did you find a solution?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks, Erel. So, for notifications the .SetInfo method is the must for usage, and requesting PERMISSION_POST_NOTIFICATIONS also.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
But if it needs a foreground service, but without notifications - it is not possible, as i can understand, as at least one notification must be made for service creation.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top