iOS Question What is the preferred methods to get instant Notifications on iOS just like Android?

Kevin Hartin

Active Member
Licensed User
Longtime User
Ive got notifications being received by my B4i app and I'm putting them into a list that I store, however I'm unable to have a notification pop up like in the android app.

Following is the code thats working in the Android app;
FirebaseMessaging:
Sub fm_MessageArrived (Message As RemoteMessage)
    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 n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_HIGH)
    n2.Icon = "icon"
    n2.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n2.Notify(1)
End Sub

I have tried to get a similar thing going in B4i with the following code, which throws no errors but doesnt notify;
FirebaseMessaging:
Public Sub MessageReceivedWhileInTheForeground (Message As Map)
    Dim aps As Map = Message.Get("aps")
    Dim alert As Map = aps.Get("alert")
    Dim n2 As Notification
    n2.Initialize(1)
    n2.PlaySound = True   
    n2.AlertBody = alert.Get("body")
    n2.NotificationTag = alert.Get("title")
End Sub

As said earlier, I am receiving the push message and can store it in a list of maps, but no sound or message pops up.

Any help appreciated,
Kev
 

Kevin Hartin

Active Member
Licensed User
Longtime User
You can use UserNotificationCenter to create notifications that show when the app is in the foreground: https://www.b4x.com/android/forum/threads/usernotificationcenter-class.117925/#content
Thanks Erel, it works a treat with the following;
UserNotifications:
    App.RegisterUserNotifications(True, True, True)
    UNC.Initialize 'after calling App.RegisterUserNotifications
    UNC.SetCategoryActions("Category 1", Array("Action 1", "Action 2", "Action 3"))
    UNC.CreateNotificationWithContent(alert.Get("title"), alert.Get("body"), "identifer 2", "Category 1", 4000)
'    UNC.RemovePendingNotifications(Array("identifer 2"))

However, I am a little confused about the Category and Actions.

I assume RemovePending will delete it from Notifications.

Kev
 
Upvote 0
Top