我正在开发Swift中的iOS应用程序。
抱歉,我的英语非常糟糕。
我想在UILocalNotification上执行任何操作,设备上的fireDate被锁定或应用程序是后台。
我已经尝试将进程写入didReceiveLocalNotification。如果设备已解锁且应用程序是前台,则行为正确,但在其他情况下,它不起作用。
我该如何实施?
请帮助我!
感谢。
答案 0 :(得分:0)
尝试此解决方案
在viewController.swift中查看[查看控制器的计划Localnotification]
func scheduleLocal() {
let settings = UIApplication.sharedApplication(). currentUserNotificationSettings()
if settings!.types == .None {
let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
return
}
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 15)
notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
notification.alertAction = "be awesome!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["CustomField1": "w00t"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
在ViewDidLoad()
中 self.scheduleLocal()
在applicationdidFinishlaunching()
中 let notificationSettings = UIUserNotificationSettings(forTypes:[.Alert, .Badge, .Sound] , categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
if let options = launchOptions {
if let notification = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
if let userInfo = notification.userInfo {
let customField1 = userInfo["CustomField1"] as! String
// do something neat here
}
}
}
最后在appdelegate.swift
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if let userInfo = notification.userInfo {
let customField1 = userInfo["CustomField1"] as! String
print("didReceiveLocalNotification: \(customField1)")
}
}
尝试并使用xcode 7.3.1 swift 2.2