Android Question Notification persists (b4a 8.0 - android 8)

yiankos1

Well-Known Member
Licensed User
Longtime User
Hello my friends,
I use this code for firebase notification but even if i click notification, it does not hide. Moreover i can not dismiss it and always pesists, till OS throws a messages that my app is running on my background.
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Dim fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
    Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_ALWAYS
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("ypiresia") 'you can subscribe to more topics
    fm.SubscribeToTopic("anakoinosi")
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
    Sleep(0) 'allow the MessageReceived event to be raised.
    Service.StopAutomaticForeground
    StopService(Me)
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize2(n.IMPORTANCE_DEFAULT)
    n.AutoCancel=True
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    Service.StartForeground(1,n)
End Sub

Sub Service_Destroy

End Sub

p.s. notification persist AND on lower android versions
p.s.1 Huawei P10 with 8.0.0
 
Last edited:

yiankos1

Well-Known Member
Licensed User
Longtime User
Assuming that you are just trying to show a notification then there are several mistakes here:
1. Remove this line:
B4X:
Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_ALWAYS
2. Remove this line:
B4X:
Service.StartForeground(1,n)
3. Call n.Notify
i fixed all of them. i updated all needed sdk. But on oreo device huawei p10 keeps coming two notifications. First notification is exactly as i want. The second one is a sticky notification, that can not swipped but if i click it, throws me on main screen of app and notification disappears after that. That notification has title and body text just like my app's name
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you can test it on the emulator if you don't have an Android 8 device.

The second one is a sticky notification, that can not swipped but if i click it, throws me on main screen of app and disappears after that.
The problem is here:
B4X:
 If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
Your code returns before it stops the automatic foreground mode.

Correct code: https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/
 
Upvote 0
Top