首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(ios10.2)(swift3)如何判断用户是否点击了本地通知?

(ios10.2)(swift3)如何判断用户是否点击了本地通知?
EN

Stack Overflow用户
提问于 2017-02-05 03:17:12
回答 2查看 2K关注 0票数 3

我做了一个应用程序,当你点击一个按钮时,它会在一段时间后发送通知。此通知在ViewController中创建。如何让我的应用程序在用户单击通知后执行某些操作?我使用的是swift 3,而不是UILocalNotification。

EN

回答 2

Stack Overflow用户

发布于 2017-02-05 03:36:28

在您的应用委托中,将一个对象配置为用户通知中心的UNUserNotificationCenterDelegate并实现userNotificationCenter(_:didReceive:withCompletionHandler:)

请注意,如果用户只是关闭通知警报,则不会调用此方法,除非您还使用.customDismissAction选项配置了与此通知对应的类别(UNNotificationCategory)。

票数 3
EN

Stack Overflow用户

发布于 2017-02-05 04:09:35

iOS 10中弃用了UILocalNotification,您应该使用UserNotifications框架。

最重要的是不要忘记import UserNotifications

首先,您应该设置UNUserNotificationCenterDelegate

代码语言:javascript
复制
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        //  - Handle notification 
    }
}

而不是设置UNUserNotificationCenter的委托。

代码语言:javascript
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (accepted, _) in
            if !accepted {
                print("Notification access denied.")
            }
        }
        return true
    }

现在您可以设置通知了。

代码语言:javascript
复制
func setup(at: Date) {
        let calendar = Calendar.current
        let components = calendar.dateComponents(in: .current, from: date)
        let newComponents = DateComponents(calendar: calendar, timeZone: .current, 
        month: components.month, day: components.day, hour: components.hour, minute: components.minute)

        let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

        let content = UNMutableNotificationContent()
        content.title = "Reminder"
        content.body = "Just a reminder"
        content.sound = UNNotificationSound.default()

        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
            if let error = error {
                print("Uh oh! We had an error: \(error)")
            }
        }
    }

并最终处理它!

代码语言:javascript
复制
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.notification.request.identifier == "textNotification" {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            guard let rootVc = appDelegate.window?.rootViewController else { return }
            let alert = UIAlertController(title: "Notification", message: "It's my notification", preferredStyle: .alert)
            let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alert.addAction(action)
            rootVc.present(alert, animated: true, completion: nil)
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42044543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档