iOS Question How Detect User Tap on Notification

Ohanian

Active Member
Licensed User
Longtime User
Hi,

I'm using FCM to send push message to my app, while the app is in foreground when the message arrives it automatically fires the Application_RemoteNotification , i need to detect the user tap on notification and then decide to do something, but i can't find a way to detect the lunch condition of Application_RemoteNotification.
Is there any way to detect user click on notification?
 

Brandsum

Well-Known Member
Licensed User
In App start sub use this code to get the notification data.

B4X:
if App.LaunchOptions.containskey("UIApplicationLaunchOptionsRemoteNotificationKey") then
    Dim n as notification = App.LaunchOptions.Get("UIApplicationLaunchOptionsRemoteNotificationKey")
End if

If its a local notification use UIApplicationLaunchOptionsLocalNotificationKey
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
In App start sub use this code to get the notification data.

B4X:
if App.LaunchOptions.containskey("UIApplicationLaunchOptionsRemoteNotificationKey") then
    Dim n as notification = App.LaunchOptions.Get("UIApplicationLaunchOptionsRemoteNotificationKey")
End if

If its a local notification use UIApplicationLaunchOptionsLocalNotificationKey

Hi,

App.LaunchOptions is always null even in Application_Start or Application_RemoteNotification!
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
The Application_RemoteNotification event should fire after you click the notification and the app starts from the closed state. I have checked and its firing. If you could post the whole code where you are registering remote notification and handling the incoming notification, it will be helpful to detect any mistake and provide a solution.
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
Let me explain my problem this way :
i have a news app that sends push messages to the user on special events (like breaking news)
on the app side after receving the fcm message it opens news page and loads the news data, but if the server sends another message while user still reading the preious message it'll automatically loads the latest news , i want to prevent this and only load the latest news if the user tap on the notification.
if i put a condition on Application_RemoteNotification to ignore the message while the news page is active it solves half of the problem but it ignores the user tap also, so i want to check if the Application_RemoteNotification fired automatically or its fired with user tap.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This seems to work:
B4X:
Private Sub Application_Active
    fm.FCMConnect 
    IsActive = True
End Sub

Private Sub Application_Background
    IsActive = False
    fm.FCMDisconnect 
End Sub

Sub Application_PushToken (Success As Boolean, Token() As Byte)
    If IsActive Then
        Log("ignore")
        Return
    End If
    Log($"PushToken: ${Success}"$)
    Log(LastException)
End Sub
IsActive is a global boolean variable.
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
This seems to work:
B4X:
Private Sub Application_Active
    fm.FCMConnect
    IsActive = True
End Sub

Private Sub Application_Background
    IsActive = False
    fm.FCMDisconnect
End Sub

Sub Application_PushToken (Success As Boolean, Token() As Byte)
    If IsActive Then
        Log("ignore")
        Return
    End If
    Log($"PushToken: ${Success}"$)
    Log(LastException)
End Sub
IsActive is a global boolean variable.

Unfortunately it's not working as expected.
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
I want to ignore Application_RemoteNotification while use is on Page2 and also i want to redirect/reload Page2 if the user tap on Notification.
Here's a simplified version of my code :

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private Page2 As Page
    
    
    Private fm As FirebaseMessaging
    
    Dim IsActive As Boolean

End Sub

Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
    
    
    App.RegisterUserNotifications(True, True, True)
    App.RegisterForRemoteNotifications
    fm.Initialize("fm")
    
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub
 
Private Sub fm_FCMConnected
    fm.SubscribeToTopic("ios_news") 'add ios_ prefix to all topics
End Sub
 
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)

    If IsActive Then
        Log("ignore")
        Return
    End If
        
    Dim intID As Int = 0
    intID = Message.Get("newsid")
    NavControl.ShowPage(Page2)
    
    CompletionHandler.Complete
      
End Sub

Private Sub Application_Active
    fm.FCMConnect 'should be called from Application_Active
    IsActive = True
End Sub

Private Sub Application_Background
    IsActive = False
    fm.FCMDisconnect 'should be called from Application_Background
End Sub

Sub Application_PushToken (Success As Boolean, TOKEN() As Byte)
 
End Sub

Sub Application_Inactive
     
End Sub

Sub Application_Foreground
 
End Sub
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
In which way doesn't it work? Also post the logs.

I think the main problem is how push works on ios if I ignore the second message I'll lose it because there isn't any notification for the second message.
 
Upvote 0
Top