我使用UIDatePicker设置通知。当我在手机上运行应用程序时,通知无效。
有什么问题?
我的代码:
import UIKit
import UserNotifications
class ViewController: UIViewController {
@IBOutlet weak var datePicker: UIDatePicker!
var timer = Timer()
var time = 10
override func viewDidLoad() {
super.viewDidLoad()
scheduleLocalNotification()
}
func scheduleLocalNotification() {
let notification = UILocalNotification()
let dateTime = datePicker.date
notification.alertAction = "Call"
notification.alertBody = "You have a call right now"
notification.fireDate = dateTime
notification.timeZone = NSTimeZone.default
UIApplication.shared.scheduleLocalNotification(notification)
}
@IBAction func pushNotification(_ sender: AnyObject) {
let AlertView = UIAlertController(title: "Time for your call!", message: "Press go to continue", preferredStyle: UIAlertControllerStyle.alert)
AlertView.addAction(UIAlertAction(title: "Go", style: UIAlertActionStyle.default, handler: nil))
self.present(AlertView, animated: true, completion: nil)
}
}
答案 0 :(得分:3)
iOS 8 - iOS9:
正如@FrederikFrandsen所说,您必须先注册您的应用程序以获取用户通知:
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
然后,您可以通过实施以下AppDelegate
功能来处理UIApplicationDelegate
中的本地通知:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
// handle your local notification here
}
iOS 10:
在AppDelegate中添加新的UNUserNotificationCenterDelegate
:
import UserNotifications
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
}
设置UNUserNotificationCenter
的代理人(例如在applicationDidFinishLaunching
中):
UNUserNotificationCenter.current().delegate = self
请求授权:
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (success, error) in
// handle error
})
触发用户通知:
let components = NSCalendar.current.dateComponents([.hour, .minute], from: datePicker.date)
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.sound = UNNotificationSound.default()
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let request = UNNotificationRequest(identifier: "myRequest", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
// handle error
})
答案 1 :(得分:1)
在iOS 10中,不推荐使用UILocalNotification。你将无法获得它的工作财产。您需要迁移代码才能使用新的用户通知框架。
答案 2 :(得分:0)
在AppDelegate.swift中
你需要这个:
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
UIApplication.shared.registerForRemoteNotifications()