Android Question B4A Firebase Push Notifications NB6 formating

Hi

I am using following event to received data from FCM receiver and build the notification with nb6 library, but i am not geting csbuilder formated text in notification body and title etc rather i am getting plain bare text in notification.


Thanks.

Sub fm_MessageArrived (Message As RemoteMessage):
Sub fm_MessageArrived (Message As RemoteMessage)
    'Log($"Message.GetData data: ${Message.GetData}"$)
    'Message.GetData data: {Notchnlid=FCMCHidIMPAlert, Notchnlnm=FCMAlerting Messages, SubText=Device 3 : Started at 20:17:59, ID=23, Text=['Device 3 : Started at 19:49:30', 'Device 3 : Stopped at 19:50:02', 'Device 4 : Started at 19:50:10', 'Device 4 : Stopped at 19:50:18', 'Overhead Pump : Started at 20:02:22 > On Time 01:10:00', 'Overhead Pump : Stopped at 20:02:34 > On duration 00:00:12', 'Device 3 : Started at 20:14:56', 'Device 3 : Stopped at 20:15:15', 'Device 3 : Started at 20:17:59'], State=1, Title=Device 3, NoTFType=2, Device=3}
    If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
        Log("App is in the foreground. In iOS a notification will not appear while the app is in the foreground (unless UserNotificationCenter is used).")
    End If
  
      
    Dim FCMsmiley As Bitmap 
    Dim n As NB6
    Dim cs As CSBuilder
  
    FCMsmiley = LoadBitmapResize(File.DirAssets, "logo-02.png", 24dip, 24dip, False)
    n.Initialize(Message.GetData.Get("Notchnlid"), Message.GetData.Get("Notchnlnm"), "DEFAULT").AutoCancel(True).SmallIcon(FCMsmiley)
    n.AutoCancel(True) ' added latest
    n.OnlyAlertOnce(True) 
    n.SubText(cs.Initialize.BackgroundColor(Colors.Yellow).Append("Sub text").PopAll)
    Dim title As Object = cs.Initialize.Color(Colors.Green).Append(Message.GetData.Get("title").As(String)).PopAll
    Dim Content As Object = cs.Initialize.Underline.Bold.Color(Colors.Magenta).Append(Message.GetData.Get("body").As(String)).PopAll
    'Dim largeIcon As Bitmap = LoadBitmapResize(File.DirAssets, "logo-02.png", 256dip, 256dip, True)
    'n.LargeIcon(largeIcon)
    n.Color(Colors.Blue)
    n.BadgeIconType("SMALL").Number(Message.GetData.Get("ID").As(Int))
    n.ShowWhen(DateTime.Now)
    'n.Build(title, Content, "tag2", Main).Notify(Message.GetData.Get("ID").As(Int))
    Dim FCMNBB As Notification
    FCMNBB = n.Build(title, Content, "tag2", Main)
    FCMNBB.Notify(Message.GetData.Get("ID").As(Int))
End Sub
 
Last edited:
Thanks Erel for reply.

I modified below 3 lines but still plain text message is displayed not nb6 formated.

Note variable Content & title title has PopAll added at last

Modified line:
Dim title As Object = cs.Initialize.Color(Colors.Green).Append(Message.GetData.Get("title").As(String)).PopAll
Dim Content As Object = cs.Initialize.Underline.Bold.Color(Colors.Magenta).Append(Message.GetData.Get("body").As(String)).PopAll
    ....
FCMNBB = n.Build(title.As(CSBuilder).PopAll, Content.As(CSBuilder).PopAll, "tag2", Main)



Sub fm_MessageArrived (Message As RemoteMessage)
    'Log($"Message.GetData data: ${Message.GetData}"$)
    'Message.GetData data: {Notchnlid=FCMCHidIMPAlert, Notchnlnm=FCMAlerting Messages, SubText=Device 3 : Started at 20:17:59, ID=23, Text=['Device 3 : Started at 19:49:30', 'Device 3 : Stopped at 19:50:02', 'Device 4 : Started at 19:50:10', 'Device 4 : Stopped at 19:50:18', 'Overhead Pump : Started at 20:02:22 > On Time 01:10:00', 'Overhead Pump : Stopped at 20:02:34 > On duration 00:00:12', 'Device 3 : Started at 20:14:56', 'Device 3 : Stopped at 20:15:15', 'Device 3 : Started at 20:17:59'], State=1, Title=Device 3, NoTFType=2, Device=3}
    If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
        Log("App is in the foreground. In iOS a notification will not appear while the app is in the foreground (unless UserNotificationCenter is used).")
    End If
    
        
    Dim FCMsmiley As Bitmap   
    Dim n As NB6
    Dim cs As CSBuilder
    
    FCMsmiley = LoadBitmapResize(File.DirAssets, "logo-02.png", 24dip, 24dip, False)
    n.Initialize(Message.GetData.Get("Notchnlid"), Message.GetData.Get("Notchnlnm"), "DEFAULT").AutoCancel(True).SmallIcon(FCMsmiley)
    n.AutoCancel(True) ' added latest
    n.OnlyAlertOnce(True)   
    n.SubText(cs.Initialize.BackgroundColor(Colors.Yellow).Append("Sub text").PopAll)
    Dim title As Object = cs.Initialize.Color(Colors.Green).Append(Message.GetData.Get("title").As(String)).PopAll
    Dim Content As Object = cs.Initialize.Underline.Bold.Color(Colors.Magenta).Append(Message.GetData.Get("body").As(String)).PopAll
    'Dim largeIcon As Bitmap = LoadBitmapResize(File.DirAssets, "logo-02.png", 256dip, 256dip, True)
    'n.LargeIcon(largeIcon)
    n.Color(Colors.Blue)
    n.BadgeIconType("SMALL").Number(Message.GetData.Get("ID").As(Int))
    n.ShowWhen(DateTime.Now)
    'n.Build(title, Content, "tag2", Main).Notify(Message.GetData.Get("ID").As(Int))
    Dim FCMNBB As Notification
    FCMNBB = n.Build(title.As(CSBuilder).PopAll, Content.As(CSBuilder).PopAll, "tag2", Main)
    FCMNBB.Notify(Message.GetData.Get("ID").As(Int))
End Sub
 
Upvote 0
There is an example of using CSBuilder with NB6: NB6 - Notifications Builder

This is how it looks on Android 14:

View attachment 156518

It works, however the text color doesn't change. The exact behavior depends on the OS.
Hello Erel,

Above NB6 formated notification is well formated only when app is in foreground, if app is closed and not running then only plain text message is received.

Might be like this , NB6 library cant be used in firebase receivers
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
NB6 doesn't actually show the notification. It is the OS that shows the notification. There is no reason for it not to work from a receiver and it indeed works:
B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(LoadBitmap(File.DirAssets, "smiley.png"))
    Dim cs As CSBuilder
    n.SubText(cs.Initialize.BackgroundColor(Colors.Yellow).Append("Sub text").PopAll)
    Dim title As Object = cs.Initialize.Color(Colors.Green).Append(Message.GetData.Get("title")).PopAll
    Dim Content As Object = cs.Initialize.Underline.Bold.Append(Message.GetData.Get("body")).PopAll
    Dim largeIcon As Bitmap = LoadBitmapResize(File.DirAssets, "logo-02.png", 256dip, 256dip, True)
    n.LargeIcon(largeIcon)
    n.Color(Colors.Blue)
    n.BadgeIconType("SMALL").Number(1)
    n.ShowWhen(DateTime.Now)
    n.Build(title, Content, "tag2", Main).Notify(5)
End Sub

 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…