杀死(完全关闭)应用程序后,UILocalNotificationAction无效

时间:2015-12-09 17:42:28

标签: ios swift uilocalnotification

我在我的应用程序上使用函数实现了UILocalNotification并且当应用程序在后台运行时工作正常但是当我完全关闭应用程序时会显示通知但是与之关联的功能没有做任何事情,如果有人知道的话我做错了什么或如何正确地做而不是帮助我

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

1 个答案:

答案 0 :(得分:0)

要在应用未打开时处理本地通知,您必须检查application:didFinishLaunchingWithOptions:中的启动选项字典。本地通知将在此词典中发送给您。

根据the documentation for application:didReceiveLocalNotification:

  

如果用户选择在发生本地通知时打开应用程序,则传递给应用程序的启动选项字典:willFinishLaunchingWithOptions:和application:didFinishLaunchingWithOptions:methods包含UIApplicationLaunchOptionsLocalNotificationKey键。在委托的应用程序之后的某个时刻调用此方法:didFinishLaunchingWithOptions:method。

以下是一个如何处理一般意义上的本地通知的示例:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        let notificationSettings = UIUserNotificationSettings(forTypes: [.Sound, .Alert], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

        if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
            let alertController = UIAlertController(title: "Notification", message: "A notification was received while the app was not running, and the user launched the app", preferredStyle: .Alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
            window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
        }

        return true
    }

    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        let alertController = UIAlertController(title: "Notification", message: "A notification was received while the app was running", preferredStyle: .Alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
    }

    var window: UIWindow?
}