Android Question Version safe notification issue

QtechLab

Active Member
Licensed User
Longtime User
Hi there,

I'm trying to update my app to the new notification system.
I copy the notification code from Erel's Post, adding it to my FirebaseMessaging service. After set 26 as sdk destination in the manifest i have this issue:

The notification service will show a second notification without any custom text (only the app name).
This notification is of "no-sound" category, and as the code suggests, IMPORTANCE_LOW.
This notification is also not deletable. How can i solve this problem?

EDIT: Solved by adding Service.StopAutomaticForeground after the n.notify

FirebaseMessaging service
B4X:
#Region  Service Attributes
    #StartAtBoot: true
 
#End Region

Sub Process_Globals
    Public fm As FirebaseMessaging
End Sub

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

Public Sub SubscribeToTopics(Family As String)
    fm.SubscribeToTopic(Family) 'you can subscribe to more topics
    Log(Family)
    Log (fm.Token)
End Sub

Public Sub UnsuscribeFromTopics(Family As String)
    fm.UnsubscribeFromTopic(Family)
    Log(Family)
    Log(fm.Token)
End Sub

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

Sub fm_TokenRefresh (Token As String)
    Log("New Token: " & Token)
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim Title As String = Message.GetData.Get("title")
    Dim Body As String = Message.GetData.Get("body")

    Dim n As Notification = CreateNotification(Title, Body, "notificon", Main, True, True)
    n.Notify(DateTime.Now)
End Sub

Sub Service_Destroy

End Sub


Private Sub CreateNotification(Title As String, Content As String, Icon As String, TargetActivity As Object, Sound As Boolean, Vibrate As Boolean) As Notification
    Dim p As Phone
    If p.SdkVersion >= 21 Then
        Dim nb As NotificationBuilder
        nb.Initialize
        nb.DefaultSound = Sound
        nb.DefaultVibrate = Vibrate
        nb.ContentTitle = Title
        nb.ContentText = Content
        nb.setActivity(TargetActivity)
        nb.SmallIcon = Icon
        nb.DefaultLight = True
        nb.OnGoingEvent = False
     
        If p.SdkVersion >= 26 Then
            Dim ctxt As JavaObject
            ctxt.InitializeContext
            Dim manager As JavaObject
            manager.InitializeStatic("android.app.NotificationManager")
            Dim Channel As JavaObject
            Dim importance As String
            If Sound Then importance = "IMPORTANCE_DEFAULT" Else importance = "IMPORTANCE_LOW"
            Dim ChannelVisibleName As String = Application.LabelName
            Channel.InitializeNewInstance("android.app.NotificationChannel", Array("MyChannelId1", ChannelVisibleName, manager.GetField(importance)))
            manager = ctxt.RunMethod("getSystemService", Array("notification"))
            manager.RunMethod("createNotificationChannel", Array(Channel))
            Dim jo As JavaObject = nb
            jo.RunMethod("setChannelId", Array("MyChannelId1"))
        End If
        Return  nb.GetNotification
    Else
        Dim n As Notification
        n.Initialize
        n.Sound = Sound
        n.Vibrate = Vibrate
        n.Icon = Icon
        n.SetInfo(Title, Content, TargetActivity)
        Return n
    End If
End Sub
 

Attachments

  • Screenshot_20180702-105222.png
    Screenshot_20180702-105222.png
    249.4 KB · Views: 157
  • Screenshot_20180702-105645.png
    Screenshot_20180702-105645.png
    54.8 KB · Views: 164
Last edited:
Top