【问题标题】:(ios10.2)(swift3) How to tell if a user has clicked on a local notification?(ios10.2)(swift3) 如何判断用户是否点击了本地通知?
【发布时间】:2017-02-04 19:17:11
【问题描述】:

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

【问题讨论】:

  • 不使用 UILocalNotification 是指使用 UNNotification?

标签: ios swift3 xcode8 localnotification


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    UILocalNotification 在 iOS 10 中已弃用。您应该改用 UserNotifications 框架。

    首先不要忘记import UserNotifications

    首先您应该设置UNUserNotificationCenterDelegate

    extension AppDelegate: UNUserNotificationCenterDelegate {
        func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            //  - Handle notification 
        }
    }
    

    比设置UNUserNotificationCenter的委托。

    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
        }
    

    现在您可以设置通知了。

    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)")
                }
            }
        }
    

    最后处理它!

    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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 2013-12-27
      • 1970-01-01
      • 2011-04-10
      相关资源
      最近更新 更多