Android Question NB6 notification, Couldn't update icon

Fadi-l

Member
Hi ...
I'm trying to use NB6 instead of Built-in notification
I put this code a Service Start
B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    Dim n As NB6
    If n.IsInitialized = False Then
        Private icon As Bitmap
        icon = LoadBitmapResize(File.DirAssets, "test.png", 24dip, 24dip, False)
        n.Initialize("default", Application.LabelName, "LOW").AutoCancel(False).SmallIcon(icon)
        Service.StartForeground(100, n.Build("Test1", "test ..... 1", "", Main))
    Else
        n.Build("Test 2", "test ..... 2", "", Main).Notify(100)
    End If
End Sub

the first time notification appear with icon and every things OK
But second time when I open the app and the service start again, gives me this error message and the service stop.

B4X:
** Service (testservice) Start **
android.app.RemoteServiceException: Bad notification posted from package b4a.example: Couldn't update icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=b4a.example id=0x00000000) visible user=0 )
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2019)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7225)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)


Where is the problem?

test project attached

Thank you ...
 

Attachments

  • test.zip
    473.6 KB · Views: 188

Erel

B4X founder
Staff member
Licensed User
Longtime User
Better to write it like this:
B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    Dim icon As Bitmap = LoadBitmapResize(File.DirAssets, "test.png", 24dip, 24dip, False)
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "LOW").AutoCancel(False).SmallIcon(icon)
    Service.StartForeground(100, n.Build("Test1", "test ..... 1", "", Main))
End Sub
n.IsInitialized will always be False on line #4 in your code.

I didn't get any error. I've rotated the device to cause the service to be started again and again.

However there is no reason to call StartForeground multiple times. Call it once in Service_Create.
 
Upvote 0

Fadi-l

Member
hi ..
thank you for the replay

I changed the code to be like this:
B4X:
Sub Process_Globals
    Dim n As NB6
    Dim icon As Bitmap
    Dim num As Int = 1
End Sub

Sub Service_Create
    icon = LoadBitmapResize(File.DirAssets, "test.png", 24dip, 24dip, False)
    n.Initialize("default", Application.LabelName, "LOW").AutoCancel(False).SmallIcon(icon)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)

    StartServiceAt("TestService", DateTime.Now+(60*1000), True)
    
    num = num +1
    n.Build("Test" & num, "test ..... " & num, "", Main).Notify(100)
End Sub


Test it on note4 (6.1) first time worked fine, after 1 minute when service start again, I've got this error:

B4X:
android.app.RemoteServiceException: Bad notification posted from package b4a.example: Couldn't update icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=b4a.example id=0x00000000) visible user=0 )
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2019)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7225)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)


but I found the problem:
if the notification appear, and the user didn't remove it, When the second time the service start, the above error will show,
But if the user remove the notification before the next service start , will not show any error

are there any way to fix this problem ?

Thank you

new test project attached
 

Attachments

  • test.zip
    501.3 KB · Views: 197
Upvote 0

Fadi-l

Member
B4X:
Dim notif As Notification =  n.Build("Test" & num, "test ..... " & num, "", Main)
notif.Cancel(100)
Sleep(50)
notif.Notify(100)

good idea, but how to avoid vibrating each time the service start ?
and does it work with Service.StartForeground ?
 
Upvote 0

Fadi-l

Member
yes Erel, but I told you, in Post #3

if the notification appear, and the user didn't remove it, When the second time the service start, the above error will show,
But if the user remove the notification before the next service start , will not show any error


so if it foreground, that mean will not remove, so if the user open the app, or the service start again, will get en error

I attached the example with your code
open the app first time, the notification start normally
close the app and open it again without remove the notificatinon, you will get an error (I've got an error on Note4)
 

Attachments

  • test.zip
    473.1 KB · Views: 194
Upvote 0

Fadi-l

Member
ok could you please correct my code, because always getting the same error

B4X:
Sub Process_Globals
    Dim num As Int = 1
    Dim n As NB6
End Sub

Sub Service_Create
    Dim icon As Bitmap = LoadBitmapResize(File.DirAssets, "test.png", 24dip, 24dip, False)

    n.Initialize("default", Application.LabelName, "LOW").AutoCancel(False).SmallIcon(icon)
    Service.StartForeground(100, n.Build("Test" & num, "test ..... "  & num, "", Main))
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    
    num = num+1
    n.Build("Test" & num, "test ..... "  & num, "", Main).Notify(100)
End Sub


I need :
the service foreground
the icon appear in the notification
update the notification info
the service will start repeatedly at the day
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code will work on most devices. On some devices you cannot update the notification without canceling the previous one.

You can try this example: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content
I guess that it will work if you remove the small icon.

Another option is to have two notifications. The second one will have an importance level of MIN and you will be able to cancel and create a new one each time.
 
Upvote 0

Fadi-l

Member
I tried the example, but the icon doesn't appear,
what do you mean if I remove the small icon ?

can I show a notification without an icon ?
 
Upvote 0
Top