Android Question Push Notification - Pop Up

interskala

Member
Licensed User
Hello, all,

The code below successfully sends a notification message from firebase cloud messaging. But it can't pop up like the whats app message.
Can someone suggest how to :
1. Bring up a pop up when the program reads incoming messages from firebase cloud messaging without having to lower the notification first.
2. When the notification is clicked, it immediately disappears from the notification list

Thank you for your help!


Sub fm_MessageArrived (Message As RemoteMessage)
Log("Message arrived")
Log($"Message data: ${Message.GetData}"$)
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
n.Notify(1)
End Sub
 

Attachments

  • test notif.jpeg
    test notif.jpeg
    33.1 KB · Views: 420

Peter Simpson

Expert
Licensed User
Longtime User
It works fine with NB6 @interskala

In Activity
B4X:
Sub Activity_Resume
    Dim IntTag As Intent = Activity.GetStartingIntent

    If IntTag.HasExtra("Notification_Tag") Then
        Log($"XTRA = ${IntTag.GetExtra("Notification_Tag")}"$)
    End If
End Sub
In Service
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Private FM As FirebaseMessaging
End Sub

Sub Service_Create
    FM.Initialize("FM")
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+. Call this when the background task completes (if there is one)
End Sub

Public Sub SubscribeToTopics
    FM.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub FM_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
     
    Dim NB As NB6
        NB.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(Application.Icon)
        NB.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), "Notification Tag", Main).Notify(1) 'It will be Main (or any other activity) instead of Me if called from a service.
         NB.OnlyAlertOnce(True)
End Sub

Enjoy...
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Try adding
B4X:
n.AutoCancel = True
1580871097500.png


The code may look like this:
B4X:
    Dim n As Notification
    n.Initialize   
    n.Icon = "icon"
    n.AutoCancel = True       
    n.SetInfo(title, body, Main)
    n.Notify(1)
 
Upvote 0

interskala

Member
Licensed User

Attachments

  • aeric.png
    aeric.png
    121.4 KB · Views: 239
Upvote 0

interskala

Member
Licensed User
It works fine with NB6 @interskala

In Activity
B4X:
Sub Activity_Resume
    Dim IntTag As Intent = Activity.GetStartingIntent

    If IntTag.HasExtra("Notification_Tag") Then
        Log($"XTRA = ${IntTag.GetExtra("Notification_Tag")}"$)
    End If
End Sub
In Service
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Private FM As FirebaseMessaging
End Sub

Sub Service_Create
    FM.Initialize("FM")
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+. Call this when the background task completes (if there is one)
End Sub

Public Sub SubscribeToTopics
    FM.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub FM_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    
    Dim NB As NB6
        NB.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(Application.Icon)
        NB.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), "Notification Tag", Main).Notify(1) 'It will be Main (or any other activity) instead of Me if called from a service.
         NB.OnlyAlertOnce(True)
End Sub

Enjoy...



thanks @Peter Simpson, to clear the notification, it's clear, but to bring up a pop up, it still can't.
 

Attachments

  • peter.png
    peter.png
    121.7 KB · Views: 195
Upvote 0

interskala

Member
Licensed User
It works fine with NB6 @interskala

In Activity
B4X:
Sub Activity_Resume
    Dim IntTag As Intent = Activity.GetStartingIntent

    If IntTag.HasExtra("Notification_Tag") Then
        Log($"XTRA = ${IntTag.GetExtra("Notification_Tag")}"$)
    End If
End Sub
In Service
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Private FM As FirebaseMessaging
End Sub

Sub Service_Create
    FM.Initialize("FM")
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+. Call this when the background task completes (if there is one)
End Sub

Public Sub SubscribeToTopics
    FM.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

Sub FM_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    
    Dim NB As NB6
        NB.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(Application.Icon)
        NB.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), "Notification Tag", Main).Notify(1) 'It will be Main (or any other activity) instead of Me if called from a service.
         NB.OnlyAlertOnce(True)
End Sub

Enjoy...


Solved @Peter Simpson, i change your code from DEFAULT to HIGH

B4X:
Sub FM_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    
    Dim NB As NB6
        NB.Initialize("default", Application.LabelName, "HIGH").AutoCancel(True).SmallIcon(Application.Icon)
        NB.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), "Notification Tag", Main).Notify(1) 'It will be Main (or any other activity) instead of Me if called from a service.
         NB.OnlyAlertOnce(True)
End Sub
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Solved @Peter Simpson, i change your code from DEFAULT to HIGH

B4X:
Sub FM_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    
    Dim NB As NB6
        NB.Initialize("default", Application.LabelName, "HIGH").AutoCancel(True).SmallIcon(Application.Icon)
        NB.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), "Notification Tag", Main).Notify(1) 'It will be Main (or any other activity) instead of Me if called from a service.
         NB.OnlyAlertOnce(True)
End Sub
Thanks for letting me know...
 
Upvote 0
Top