Android Question Permanent notifications sometimes appear

Discussion in 'Android Questions' started by Alankoba, Aug 27, 2019.

  1. Alankoba

    Alankoba Member Licensed User

    Migrating from API 26 to 29 I noticed strange behavior in my app.
    No reason a notification appears permanently (cannot be deleted)
    I use Firebase notifications, but no messages have been sent to the app.


    My FirebaseMessaging.b4a

    Code:
    Sub Process_Globals
        
    Public fm As FirebaseMessaging
    End Sub

    Sub Service_Create
        fm.Initialize(
    "fm")
    End Sub

    Public Sub SubscribeToTopics
        fm.SubscribeToTopic(
    "pedidoacaigoods_android_main")
        
    Log(fm.token)
    End Sub

    Sub Service_Start (StartingIntent As Intent)
        
    If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
    End Sub

    Sub fm_MessageArrived (Message As RemoteMessage)
        
    Log("Message arrived")
        
    Log($"Message data: ${Message.GetData}"$)
        
    CallSub3(Main, "msgPush", Message.GetData.Get("title"), Message.GetData.Get("body"))
    End Sub

    Sub called in fm_MessageArrived

    Code:
    Sub msgPush(titulo As String, msg As String)
        
    Msgbox(msg, titulo)
    End Sub

    My Manifest

    Code:
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "5" android:targetSdkVersion="29"/>
    <supports-screens android:largeScreens=
    "true"
        android:normalScreens=
    "true"
        android:smallScreens=
    "true"
        android:anyDensity=
    "true"/>)
        
    AddApplicationText(
     
    <uses-library
          android:name=
    "org.apache.http.legacy"
          android:required=
    "false" />)
        

    SetActivityAttribute (Main, android:exported, 
    "true")
    SetActivityAttribute (Main, android:taskAffinity, 
    "")
    SetActivityAttribute (Main, android:theme, 
    "@android:style/Theme.DeviceDefault.Light")   

    SetApplicationAttribute(android:icon, 
    "@drawable/icon")
    SetApplicationAttribute(android:
    label"$LABEL$")
    SetApplicationAttribute(android:theme, 
    "@android:style/Theme.Holo")
    ' fix api 29
    SetApplicationAttribute(android:usesCleartextTraffic,"true")
    'End of default text.

    'Localizacao
    AddPermission (android.permission.ACCESS_FINE_LOCATION)

    CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
    CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
    CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)


    B4A 9.01.2
    FirebaseNotifications lib 1.21
     

    Attached Files:

  2. Douglas Farias

    Douglas Farias Expert Licensed User

    Code:
    Sub Service_Start (StartingIntent As Intent)
        
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    End Sub
     
    José J. Aguilar likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. Alankoba

    Alankoba Member Licensed User

    On Samsung Galaxy smartphones (S8, S9, S10 With ONE UI) the problem still occurs even after the inclusion of the Service.StopAutomaticForeground line.

    B4A Version 9.01.2

    Is it a bug?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Please post the updated code.
     
  6. Alankoba

    Alankoba Member Licensed User

    Code:
    Sub Process_Globals
        
    Public fm As FirebaseMessaging
    End Sub

    Sub Service_Create
        fm.Initialize(
    "fm")
    End Sub

    Public Sub SubscribeToTopics
        fm.SubscribeToTopic(
    "pedidoacaigoods_android_main")
        
    Log(fm.token)
    End Sub

    Sub Service_Start (StartingIntent As Intent)
        
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
        Sleep(
    0)
        
    Service.StopAutomaticForeground 'remove if not using B4A v8+.  
    End Sub

    Sub fm_MessageArrived (Message As RemoteMessage)
        
    Log("Message arrived")
        
    Log($"Message data: ${Message.GetData}"$)
        
    CallSub3(Main, "msgPush", Message.GetData.Get("title"), Message.GetData.Get("body"))
    End Sub

    ----


    Code:
    Sub msgPush(titulo As String, msg As String)
        
    Msgbox(msg, titulo)
    End Sub
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Code looks correct. You can remove the Sleep(0) it is not needed. Are there any other services in your program?

    Why have you added these lines:
    Code:
    SetActivityAttribute (Main, android:exported, "true")
    SetActivityAttribute (Main, android:taskAffinity, 
    "")
     
  8. Alankoba

    Alankoba Member Licensed User

  9. Erel

    Erel Administrator Staff Member Licensed User

    The updated code you posted will not show the automatic foreground notification. The previous code will show. Maybe you are still running an old version of your app.
     
  10. Alankoba

    Alankoba Member Licensed User

    Yes, with the fix the problem stopped, but in the Samsung Galaxy continued to happen.

    I removed the lines from the manifest and I will test again ...


    Is it possible to test without waiting at random mode?
     
  11. Alankoba

    Alankoba Member Licensed User

    The problem continues after removing the lines from the manifest. On Samsung Galaxy smartphones with Android 9.
    B4A Updated (9.50 BETA 1)
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload the project?
     
  13. Alankoba

    Alankoba Member Licensed User

    Attached
     

    Attached Files:

  14. Erel

    Erel Administrator Staff Member Licensed User

    I don't know whether this is the cause of the problem however the starter service should never be explicitly started. This means that StartAtBoot must be False:
    [​IMG]
     
  15. Alankoba

    Alankoba Member Licensed User

    In Starter.bas I made the following change:

    Code:
    Sub Service_Start (StartingIntent As Intent)
        
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    End Sub
    It seems that the problem has been solved! But turning on the device, receiving a push notification and clicking it has no effect!

    Clicking the notification only opens the app when the app has been manually opened at least once.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    You should start a new thread for a new question. Make sure to disable StartAtBoot.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice