Я реализовал UILocalNotification в своем приложении с функцией и прекрасно работал, когда приложение работает в фоновом режиме, но когда я полностью закрываю приложение, появляется уведомление, но функция, которая связано с ничего не делать, если кто-нибудь знает, что я сделал не так и как это сделать правильно, чем помочь мнеUILocalNotificationAction не работает после убийства (полностью закрывается) приложение
//AppDelegate.Swift//
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//set up Notifications//
//actions
//actions for notifications//
let firstAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
firstAction.identifier = "FIRST_ACTION"
firstAction.title = "Yes"
firstAction.activationMode = UIUserNotificationActivationMode.Background
firstAction.destructive = false
firstAction.authenticationRequired = false
let secondAction:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
secondAction.identifier = "SECOND_ACTION"
secondAction.title = "No"
secondAction.activationMode = UIUserNotificationActivationMode.Background
secondAction.destructive = true
secondAction.authenticationRequired = false
// category
let firstCategory: UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
firstCategory.identifier = "FIRST_CATEGORY"
let defaultActions:NSArray = [firstAction, secondAction]
let minimalActions:NSArray = [firstAction, secondAction]
firstCategory.setActions([firstAction, secondAction], forContext: .Default)
firstCategory.setActions([firstAction, secondAction], forContext: .Minimal)
//NSSET Of all Our Category
let categories:NSSet = NSSet(objects: firstCategory)
//asking permission for notifcation
if #available(iOS 8, *){
let mySettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: categories as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
UIApplication.sharedApplication().cancelAllLocalNotifications()
}
return true
}
func increaseScore(){
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext
let proxy = NSEntityDescription.insertNewObjectForEntityForName("Proxy", inManagedObjectContext: context) as NSManagedObject
proxy.setValue(1, forKey: "present")
proxy.setValue(NSDate(), forKey: "date")
do {
try context.save()
} catch {
print("error")
}
print(proxy)
print("Object Saved")
}
// handling actions
func application(application: UIApplication,
handleActionWithIdentifier identifier:String?,
forLocalNotification notification: UILocalNotification,
completionHandler: (() ->Void)){
if(identifier == "FIRST_ACTION"){
NSNotificationCenter.defaultCenter().postNotificationName("actionOnePressed", object: nil)
} else if(identifier == "SECOND_ACTION"){
NSNotificationCenter.defaultCenter().postNotificationName("actionTwoPressed", object: nil)
}
completionHandler()
}
//ViewController.Swift//
// setting notfication action
NSNotificationCenter.defaultCenter().addObserver(self, selector: "increaseScore", name: "actionOnePressed", object: nil)
//scheduling notification
let dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 11;
dateComp.day = 02;
dateComp.hour = 22;
dateComp.minute = 50;
dateComp.timeZone = NSTimeZone.systemTimeZone()
let calendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
let date:NSDate = calendar.dateFromComponents(dateComp)!
let notification:UILocalNotification = UILocalNotification()
notification.category = "FIRST_CATEGORY"
notification.alertBody = "hey been to college? "
notification.fireDate = date
notification.repeatInterval = NSCalendarUnit.Day
let dateNow = NSDate()
let noticalendar = NSCalendar.currentCalendar()
let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow)
if isalerViewShown == false && hour >= 15 {
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}}
Какой метод код ViewController.swift существует в? –
какой способ? @CharlesA. –