Android Question Notification is displayed when app has no notifications defined when widget is on home screen

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

My customers are noticing a notification being displayed in the notification area of their screens as soon as they place my app's widget on their home screens. The app does not have any notifications defined nor are they used in any of my coding. This seems to be tied into the widget which also does not have anything like that in use.

The notification seems persistent. They can't use "Clear" to clear out the notification.

I would assumed there was a parameter to set in ConfigureHomeWidget that controlled the notification but didn't find anything in there based on the parameter names. I also checked if there was a property I could set in RemoteView but couldn't find any.

I don't mind the notification but it's my customers who don't want to see it.

All help will be appreciated.

Thanks.

Coding from the widget:
#Region  Service Attributes
    #StartAtBoot: True
    
#End Region

Sub Process_Globals

    Private strShared As String

    Dim rv As RemoteViews
    Private kvs As KeyValueStore
End Sub

Sub Service_Create
    
    InitializeObjects
    
    rv = ConfigureHomeWidget("WidgetLayout", "rv",  1, "The Prayer Times")
End Sub

Sub Service_Start (StartingIntent As Intent)
    
    UpdateWidgetPrayerTimes
    
    If rv.HandleWidgetEvents(StartingIntent) Then
    
        Return
    End If
End Sub

Sub rv_RequestUpdate
    rv.UpdateWidget
End Sub

Sub rv_Disabled
    StopService("")
End Sub

Sub Service_Destroy

End Sub

' Misc. sub routines
'-------------------
Sub InitializeObjects   
    strShared = Starter.rp.GetSafeDirDefaultExternal("Settings")
    kvs.Initialize(strShared, "Settings")
End Sub

Sub UpdateWidgetPrayerTimes
    
    ' Display the prayer times from the database.
    '--------------------------------------------
    rv.SetText("LabelFajr", "Fajr: " & kvs.Get("FajrSalatTime"))
    rv.SetText("LabelSunrise", "Sunrise: " & kvs.Get("SunriseTime"))
    rv.SetText("LabelDhuhr", "Dhuhr: " & kvs.Get("DhuhrSalatTime"))
    rv.SetText("LabelAsr", "Asr: " & kvs.Get("AsrSalatTime"))
    rv.SetText("LabelMagrib", "Magrib: " & kvs.Get("MagribSalatTime"))
    rv.SetText("LabelIsha", "Isha: " & kvs.Get("IshaSalatTime"))
    
    ' Blue indicates it's time for wudu. Green indicates salat came in.
    ' Black is the default colour.
    '------------------------------------------------------------------
    Select kvs.Get("FajrTextColour")
        
        Case "Blue"
            rv.SetTextColor("LabelFajr", Colors.Blue)

        Case "Green"
            rv.SetTextColor("LabelFajr", Colors.RGB(49,152,16))
            
        Case "Black"
            rv.SetTextColor("LabelFajr", Colors.Black)
    End Select

    Select kvs.Get("SunriseTextColour")
        
        Case "Green"
            rv.SetTextColor("LabelSunrise", Colors.RGB(49,152,16))
            
        Case "Black"
            rv.SetTextColor("LabelSunrise", Colors.Black)
    End Select

    Select kvs.Get("DhuhrTextColour")
        
        Case "Blue"
            rv.SetTextColor("LabelDhuhr", Colors.Blue)

        Case "Green"
            rv.SetTextColor("LabelDhuhr", Colors.RGB(49,152,16))
            
        Case "Black"
            rv.SetTextColor("LabelDhuhr", Colors.Black)
    End Select

    Select kvs.Get("AsrTextColour")
        
        Case "Blue"
            rv.SetTextColor("LabelAsr", Colors.Blue)

        Case "Green"
            rv.SetTextColor("LabelAsr", Colors.RGB(49,152,16))
            
        Case "Black"
            rv.SetTextColor("LabelAsr", Colors.Black)
    End Select

    Select kvs.Get("MagribTextColour")
        
        Case "Blue"
            rv.SetTextColor("LabelMagrib", Colors.Blue)

        Case "Green"
            rv.SetTextColor("LabelMagrib", Colors.RGB(49,152,16))
            
        Case "Black"
            rv.SetTextColor("LabelMagrib", Colors.Black)
    End Select

    Select kvs.Get("IshaTextColour")
        
        Case "Blue"
            rv.SetTextColor("LabelIsha", Colors.Blue)

        Case "Green"
            rv.SetTextColor("LabelIsha", Colors.RGB(49,152,16))
            
        Case "Black"
            rv.SetTextColor("LabelIsha", Colors.Black)
    End Select

    rv.UpdateWidget
End Sub

Sub DisplaySalatCountdown (strTimeBeforeSalat As String, strFlashMode As String)

    rv.SetText("LabelSalatCountdown", strTimeBeforeSalat)

    Select strFlashMode
        Case "Flash Countdown Invisible"
            rv.SetVisible("LabelSalatCountdown", False)

        Case "Flash Countdown Visible"
            rv.SetVisible("LabelSalatCountdown", True)
            rv.SetTextColor("LabelSalatCountdown", Colors.RGB(206, 23, 23))

        Case "Normal Countdown"
            rv.SetVisible("LabelSalatCountdown", True)
            rv.SetTextColor("LabelSalatCountdown", Colors.Black)
    End Select

    rv.UpdateWidget
End Sub

' Event handlers.
'----------------
Sub PanelPrayerTimes_Click
    StartActivity(Main)
End Sub
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Is it because your widget service is running in the foreground? Do you need to add Service.StopAutomaticForeground to your Service_Start?

- Colin.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Is it because your widget service is running in the foreground? Do you need to add Service.StopAutomaticForeground to your Service_Start?

- Colin.
Greetings,

The widget service can't be stopped because it updates times on the widget so I can't use StopAutomaticForeground.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Then I suspect you're going to have to live with the notification. From https://developer.android.com/guide/components/services:


- Colin.
Hi,

I added some code which I think will work. So far so good. I will look at it tomorrow to see if the widget is still active. If it is I will mark the post as completed.

Here's the new coding I added.

New coding in Service_Start:
    Sleep(0)
    Service.StopAutomaticForeground

    ' Re-start this service every 30 seconds.
    '----------------------------------------   
    StartServiceAt(Me, DateTime.Now + (30000), True)
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User

So are you actually stopping the service somewhere? StopAutomaticForeground doesn't stop the service - just stops it running in the foreground. But you probably knew that already...

- Colin.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
So are you actually stopping the service somewhere? StopAutomaticForeground doesn't stop the service - just stops it running in the foreground. But you probably knew that already...

- Colin.
Hi Colin,

When I added StopAutomaticForground, the widget would freeze after a minute so I added the code to re-start the service and the widget kept ticking away. I will make sure it's still ticking tomorrow which I expect will be the case.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
So are you actually stopping the service somewhere? StopAutomaticForeground doesn't stop the service - just stops it running in the foreground. But you probably knew that already...

- Colin.
Hi Colin,

Using StopAutomaticForeground won't work for my app because it still gets killed even if I re-start it every 15 seconds.

Looks like I have to live with the persistent notification and let the users know it's just to let them know the app is running behind the scenes and not to worry.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Yeah - I wasn't sure that your approach would work, plus if you're distributing via Play Store I suspect the app would be rejected for trying to circumvent the requirement to display a notification.

Doing a Google search I did find this -> https://stackoverflow.com/questions...ithout-showing-notification/41177817#41177817, which might work for you - but it might not be the solution you want.

- Colin.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Colin,

At least I can tell the users, Google requires the notification to be there. I wish at least that there was a way to change the title and body text of that notification. All it shows is a duplicate title and text body with the name of my app.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Yeah I've noticed that Play Store does this on my Pixel with Android 10 on it. You just see a notification & even if you tap on it, nothing happens. The Play Store one can sometimes be dismissed though, so I guess it's a remnant from a foreground service that was running in the past but didn't remove the notification when it quit.

- Colin.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
The good thing about the B4A version of the notification is tapping on it activates the app that belongs with the notification.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…