Karan Alangat
Karan Alangat

Reputation: 2224

Detect push notification selection when the app is at running state in iOS

I am using didReceiveRemoteNotification for detecting the app notification. But it is is getting fired automatically, when the app is at running state. I need the notification selection get detected when the app is at running state, rather than automatic notification detection through didReceiveRemoteNotification. Thanks in Advance

Upvotes: 0

Views: 848

Answers (1)

Keshu R.
Keshu R.

Reputation: 5225

iOS 10+ provide the custom local notification for handling this type of issue when the app is running in the foreground.

In didFinishLaunchingWithOptions, add the delegate

    UNUserNotificationCenter.current().delegate = self

then create an appDelegate extension and add this.

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo



    // Print full message.
    print(userInfo)

    // Change this to your preferred presentation option
    completionHandler([.alert,.sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                               didReceive response: UNNotificationResponse,
                               withCompletionHandler completionHandler: @escaping () -> Void) {
       let userInfo = response.notification.request.content.userInfo


       // Print full message.
       print("tap on on forground app",userInfo)

       completionHandler()
   }

}

For details:

Read This Tutorial

Upvotes: 3

Related Questions