HomeiOS Developmentios - Is it attainable to detect if person tapped on push...

ios – Is it attainable to detect if person tapped on push notification, or launched the app by click on on the app icon, when he acquired push notification?


To detect if a person has tapped on a push notification, you possibly can implement the ‘UNUserNotificationCenterDelegate’ protocol in your ‘AppDelegate’ class and use the ‘userNotificationCenter(_:didReceive:withCompletionHandler:)’ technique.

Here is how your ‘AppDelegate’ class will seem like:

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Set UNUserNotificationCenter Delegate
        UNUserNotificationCenter.present().delegate = self
        return true
    }
    
    // Deal with notification when app is in foreground
    func userNotificationCenter(_ middle: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // Show notification when app is in foreground
        completionHandler([.banner, .sound, .badge])
    }
    
    // Deal with notification when app is in background or terminated
    func userNotificationCenter(_ middle: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        // Verify if person tapped on the notification
        if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
            print("Consumer tapped on the notification")
            // Deal with the notification
        }
        completionHandler()
    }
}

the above operate is named provided that person responds to a push notification, if it’s not known as then you possibly can contemplate the app launch by the faucet of app icon on House Display.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments