Android Question FirebaseMessaging spawns notification for a second

Nerdworld

Member
Licensed User
Longtime User
Hey folks!

Whenever my app is in background and i receive a push message (only data, without notification) my service spawns a single notification.

But for about 2 to 3 seconds there is another notification (obvisouly for the FirebaseMessaging service):

B4X:
Sub Service_Start(StartingIntent As Intent)
    Firebase.HandleIntent(StartingIntent)
    Sleep(0) ' Regardless of this sleep call
    Service.StopAutomaticForeground ' 2nd notification disappears (2 or 3 seconds too late)
End Sub

It stays until the Service.StopAutomaticForeground is called and disappears.

Can't really figure out what i'm missing here...

Related to:

Another similiar case here:

But removing the Sleep call has no effect.


Best regards
Daniel
 

Nerdworld

Member
Licensed User
Longtime User
I solved it now by calling
B4X:
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
in Service_Create.

It crashes the app after several seconds tho.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Nerdworld

Member
Licensed User
Longtime User
Hiding the full error is the best way not to get help ;-)
Sorry! Was still investigating

B4X:
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{5cd30e1 u0 com.pixelhauer.dmz/.firebasemessaging}

I think i finally solved it:

Starting the foreground service (Service.StartForeground(1, N)) in the MessageArrived sub.
 
Last edited:
Upvote 0

Nerdworld

Member
Licensed User
Longtime User
Very expected. You must read: Automatic Foreground Mode

I already fixed the issue (and resolved the problem with the two notifications showing up):

B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Dim Firebase As FirebaseMessaging
End Sub

Sub Service_Create
    Firebase.Initialize("Push")
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
End Sub

Sub Service_Start(StartingIntent As Intent)
    Firebase.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub

Sub Subscribe
    Firebase.SubscribeToTopic("enquiry")
End Sub

Sub Push_MessageArrived(Message As RemoteMessage)
    Dim N As Notification
    N.Initialize
    N.Icon = "icon"
    N.Sound = True
    N.Vibrate = True
    N.SetInfo2(Message.GetData.Get("title"), Message.GetData.Get("text"), Message.GetData.Get("tag"), Main)
    
    Service.StartForeground(1, N)
End Sub

Sub Service_Destroy
End Sub
 
Upvote 0
Top