How do I respond to a tap on a UNUserNotification?

一笑奈何 提交于 2019-12-01 09:40:31

Set AppDelegate to be UNUserNotificationCenterDelegate if you haven't already and add the following to the didReceive method:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    let userInfo = ["identifier":response.notification.request.identifier]
    NotificationCenter.default.post(name: Notification.Name(rawValue: "userNotificationCenterDidReceiveResponse"), object: self, userInfo: userInfo)

    completionHandler()
}

You can then register for that "userNotificationCenterDidReceiveResponse" notification and do whatever you like with the identifier. This works no matter what state your app is in, including not running.

If this isn't clear enough I can upload a sample project.

Use UNUserNotificationCenter Delegates.

Use this when app is in foreground:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler

get the values to use from notification

And this when app is in the background:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler

get the values to use from response.

Hope this will help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!