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: 745
This code should show the pop up
1580870303771.png


To clear the notification, make sure the Id matched.
1580870452300.png
 
Upvote 0
It works fine with NB6 @interskala

In Activity
Code:
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
Code:
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
Try adding
B4X:
n.AutoCancel = True
1580871097500.png


The code may look like this:
[CODE lang="b4x" highlight="4"] Dim n As Notification
n.Initialize
n.Icon = "icon"
n.AutoCancel = True
n.SetInfo(title, body, Main)
n.Notify(1)[/CODE]
 
Upvote 0

Attachments

  • aeric.png
    aeric.png
    121.4 KB · Views: 561
Upvote 0
It works fine with NB6 @interskala

In Activity
Code:
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
Code:
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: 598
Upvote 0
The code below successfully sends a notification message from firebase cloud messaging. But it can't pop up like the whats app message.
I may be misunderstood by your exact meaning. Do you mean the pop up will show when your app is running in foreground?
 
Upvote 0
It works fine with NB6 @interskala

In Activity
Code:
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
Code:
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

Code:
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
Solved @Peter Simpson, i change your code from DEFAULT to HIGH

Code:
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
Back
Top