Android Question [SOLVED] Placing a widget causes notification title and body to be duplicated.

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I'm having a problem where placing the app widget on the screen causes the notification title and body to be duplicated when the user pulls down the notifications drawer.

Here's the code that places the title and body of the notification which is in the starter service.

B4X:
Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.

    SetupDefaultSettingsDatabase
    InitializeObjects
    
    nNotify.Initialize2(nNotify.IMPORTANCE_LOW)
    nNotify.Icon = "icon"
    nNotify.SetInfo("My app name is here",  "This is the body text...", "")
    Service.AutomaticForegroundNotification = nNotify

    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    wakeState.PartialLock

    StartTheTimer
End Sub

The notification works perfectly only if the user doesn't put the widget on the screen. As soon as the user does that, the body and title are duplicated when the user pulls down the notifications drawer.

I can live with this but it would be nicer if the app shows the notification in a consistent way.

Please forgive me if this thread sounds similar to another one I posted earlier which I closed as solved.

Thanks
 

rleiman

Well-Known Member
Licensed User
Longtime User
Why are you setting automatic foreground mode to 'always'?

You should call Service.StopAutomaticForeground after you are done updating the widget.

Must read: Automatic Foreground Mode
Hi Erel,

I tried but still get the duplicates when the widget is placed on the screen.

B4X:
public Sub UpdateWidgetIcon

    ' Determine which image to display from the file name.
    '-----------------------------------------------------
    rv.SetImage("ImageViewWidgetIcon", LoadBitmap(File.DirAssets, kvs.Get("WidgetIconFileName")))
    rv.UpdateWidget
    Service.StopAutomaticForeground
End Sub
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I got it working by changing the coding to this.

B4X:
public Sub UpdateWidgetIcon

    ' Determine which image to display from the file name.
    '-----------------------------------------------------
    rv.SetImage("ImageViewWidgetIcon", LoadBitmap(File.DirAssets, kvs.Get("WidgetIconFileName")))
    rv.UpdateWidget
    nNotify.Initialize2(nNotify.IMPORTANCE_LOW)
    nNotify.Icon = "icon"
    nNotify.SetInfo("My app name is here",  "This is the body text...", "")
    Service.AutomaticForegroundNotification = nNotify
    Service.StopAutomaticForeground
End Sub
 
Upvote 0
Top