0

How can I handle when notification is clicked?

adding action means adding new button to notification, I don't want to add a button; I want to go to special view controller when notification is selected like builder.setContentIntent in android.

I read Managing Your App’s Notification Support but couldn't find anything.

gmds
  • 19,325
  • 4
  • 32
  • 58
Nadia
  • 117
  • 1
  • 13

1 Answers1

2

For ios 10 or later there is two new method which handles notification.

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

This Method called when user tap on notification when app is in foreground.

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

This Method called when app is in background.

And for < ios 10

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

This Method called when app is in background.(You won't able to see notification if app is in foreground but you can get notification in above method)

And For Notification Permission

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
    }

    if !UIApplication.shared.isRegisteredForRemoteNotifications {

        if #available(iOS 10, *) {
            UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
            UIApplication.shared.registerForRemoteNotifications()
        }else if #available(iOS 9, *) {
            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
 return true
 }
jay patel
  • 238
  • 2
  • 14