iOS Question Push notifications some problems

Carlos marin

Active Member
Licensed User
Longtime User
Hi, I'm trying to send push to iOS devices, I'm having a couple of problems
1. On some devices the push token is changing almost immediately after starting the app and when exiting it. I get the error in Firebase: FirebaseMessagingError: Requested entity was not found. I don't know if it's something I'm doing wrong (I send the pushes as notifications like this:
message {
token: 'fmtXOJHk3EBzv2....',
notification: {
title: 'Hello Andrea Alejandra, a registered visitor has arrived!',
body: 'waiting at entrance'
}
}
2. In the MessageReceivedWhileInTheForeground event I try to play a sound every time a push is sent but it doesn't sound until the app is opened, in fact the push is not heard
Could you tell me what I could be doing wrong? :(:(:(

main:
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
    Try
        FirebaseMessaging.MessageReceivedWhileInTheForeground(Message)
'        UNC.Initialize
'        UNC.SetCategoryActions("Category 1", Array("Action 1", "Action 2", "Action 3"))
'        UNC.CreateNotificationWithContent(Message.Get("title"), Message.Get("body"), "identifer 2", "Category 1", 4000)
        CompletionHandler.Complete
        'Msgbox(Message.Get("body"), Message.Get("title"))
    Catch
        Log(LastException)
    End Try
End Sub

Private Sub Application_PushToken (Success As Boolean, Token() As Byte)
    Log($"PushToken: ${Success}"$)
    If Success = False Then
        Log(LastException)
    End If
End Sub

Private Sub Application_Active
    FirebaseMessaging.connect
End Sub

'Template version: B4i-1.0
#Region Delegates

Private Sub Application_Background
    B4XPages.Delegate.Activity_Pause
    FirebaseMessaging.disconect
End Sub


FirebaseMessaging:
Sub Process_Globals
    Private fm As FirebaseMessaging
    Private FirstTime As Boolean = True
    Dim mp1 As MediaPlayer
End Sub

Public Sub SubscribeToTopics (Topics() As Object)
    If FirstTime Then
        FirstTime = False
        fm.Initialize("fm")
        fm.FCMConnect
        Wait For fm_FCMConnected
        Log("FCMConnected")
    End If
    For Each topic As String In Topics
        fm.SubscribeToTopic("ios_" & topic)
    Next
    Log(GetToken)
    Try
        Dim token As String = GetToken
        If token <> "" Then
            If comCode.fmToken <> token Then
                comCode.fmToken = token
                Starter.update_token(token)
            End If
        End If
    Catch
        Log(LastException)
    End Try
    
    
End Sub

Public Sub disconect
    fm.FCMDisconnect
End Sub

Public Sub connect
    fm.FCMConnect
End Sub

Private Sub GetToken As String
    Dim FIRMessaging As NativeObject
    FIRMessaging = FIRMessaging.Initialize("FIRMessaging").GetField("messaging")
    Dim token As NativeObject = FIRMessaging.GetField("FCMToken")
    If token.IsInitialized = False Then Return "" Else Return token.AsString
End Sub

Public Sub MessageReceivedWhileInTheForeground (Message As Map)
    Dim FileName As String = "ringtone1.mp3"
    mp1.Initialize(File.DirAssets, FileName,"mp1")
    mp1.Play
'    Dim aps As Map = Message.Get("aps")
'    Dim alert As Map = aps.Get("alert")
'    Log(alert.Get("body"))
'    Log(alert.Get("title"))
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Might be simpler to use topics instead of tokens. You can assign a unique topic to each user (as far as I remember there is no limit to the number of topics).

In the MessageReceivedWhileInTheForeground event I try to play a sound every time a push is sent but it doesn't sound until the app is opened, in fact the push is not heard
Could you tell me what I could be doing wrong?
This is the expected behavior. Your app doesn't handle the notification while it is in the background. It is the OS that shows the notification (unlike in Android).
 
Upvote 0
Top