Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于iOS通知_Ios_Swift2 - Fatal编程技术网

关于iOS通知

关于iOS通知,ios,swift2,Ios,Swift2,我正在研究通知,我创建了一个代码,使用datePicker来计划显示通知。代码运行得很好。 但是,当我的应用程序在后台时,如何每10分钟显示一次通知? 这是我的操作系统代码: AppDelegate: func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { print(not

我正在研究通知,我创建了一个代码,使用datePicker来计划显示通知。代码运行得很好。 但是,当我的应用程序在后台时,如何每10分钟显示一次通知? 这是我的操作系统代码:

AppDelegate:

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {  

    print(notificationSettings.types.rawValue)  
}  

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {  
    print("Received Local Notification:")  
    print(notification.alertBody!)  
}  

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {  

    if identifier == "editList" {  
        NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil)  
    }  
    else if identifier == "trashAction" {  
        NSNotificationCenter.defaultCenter().postNotificationName("deleteListNotification", object: nil)  
    }  

    completionHandler()  
}  
视图控制器:

import UIKit  
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {  
@IBOutlet weak var txtAddItem: UITextField!  

@IBOutlet weak var tblShoppingList: UITableView!  

@IBOutlet weak var btnAction: UIButton!  

@IBOutlet weak var datePicker: UIDatePicker!  
var shoppingList: NSMutableArray!  

var timer = NSTimer()  



override func viewDidLoad() {  
    super.viewDidLoad()  

    self.tblShoppingList.delegate = self  
    self.tblShoppingList.dataSource = self  

    self.txtAddItem.delegate = self  

    datePicker.hidden = true  

    loadShoppingList()  
    setupNotificationSettings()  

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleModifyListNotification), name: "modifyListNotification", object: nil)  
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleDeleteListNotification), name: "deleteListNotification", object: nil)  

    listenerSchedule()  


}  


override func didReceiveMemoryWarning() {  
    super.didReceiveMemoryWarning()  
}  

func handleModifyListNotification() {  
    txtAddItem.becomeFirstResponder()  
}  

func handleDeleteListNotification() {  
    shoppingList.removeAllObjects()  
    saveShoppingList()  
    tblShoppingList.reloadData()  
}  

func setupNotificationSettings() {  

    let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()  

    if (notificationSettings.types == UIUserNotificationType.None){  

        let notificationTypes = UIUserNotificationType.Alert.union(UIUserNotificationType.Sound)  

        let justInformAction = UIMutableUserNotificationAction()  
        justInformAction.identifier = "justInform"  
        justInformAction.title = "OK, got it"  
        justInformAction.activationMode = UIUserNotificationActivationMode.Background  
        justInformAction.destructive = false  
        justInformAction.authenticationRequired = false  

        let modifyListAction = UIMutableUserNotificationAction()  
        modifyListAction.identifier = "editList"  
        modifyListAction.title = "Edit list"  
        modifyListAction.activationMode = UIUserNotificationActivationMode.Foreground  
        modifyListAction.destructive = false  
        modifyListAction.authenticationRequired = true  

        let trashAction = UIMutableUserNotificationAction()  
        trashAction.identifier = "trashAction"  
        trashAction.title = "Delete list"  
        trashAction.activationMode = UIUserNotificationActivationMode.Background  
        trashAction.destructive = true  
        trashAction.authenticationRequired = true  

        let actionsArray = NSArray(objects: justInformAction, modifyListAction, trashAction)  
        let actionsArrayMinimal = NSArray(objects: trashAction, modifyListAction)  

        let shoppingListReminderCategory = UIMutableUserNotificationCategory()  
        shoppingListReminderCategory.identifier = "shoppingListReminderCategory"  
        shoppingListReminderCategory.setActions(actionsArray as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)  
        shoppingListReminderCategory.setActions(actionsArrayMinimal as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)  


        let categoriesForSettings = NSSet(objects: shoppingListReminderCategory)  


        let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as? Set<UIUserNotificationCategory>)  
        UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)  
    }  

}  

func setActions(actions: [AnyObject]!, forContext context: UIUserNotificationActionContext){}  

func scheduleLocalNotification() {  
    let localNotification = UILocalNotification()  
    localNotification.fireDate = fixNotificationDate(datePicker.date)
    localNotification.alertBody = "test test test"  
    localNotification.alertAction = "View List"  
    localNotification.category = "shoppingListReminderCategory"  

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)  
}  

func listenerSchedule() {  
    timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: #selector(ViewController.startNotication), userInfo: nil, repeats: true)  
}  

func startNotication(){  

}  

func fixNotificationDate(dateToFix: NSDate) -> NSDate {  

    let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([.Day, .Month, .Year, .Hour, .Minute], fromDate: dateToFix)  

    dateComponets.second = 0  

    let fixedDate: NSDate! = NSCalendar.currentCalendar().dateFromComponents(dateComponets)  

    return fixedDate  
}  

func textFieldShouldReturn(textField: UITextField) -> Bool {  
    if shoppingList == nil{  
        shoppingList = NSMutableArray()  
    }  
    shoppingList.addObject(textField.text!)  

    tblShoppingList.reloadData()  

    txtAddItem.text = ""  
    txtAddItem.resignFirstResponder()  

    saveShoppingList()  

    return true  
}  

@IBAction func scheduleReminder(sender: AnyObject) {  
    if datePicker.hidden {  
        animateMyViews(tblShoppingList, viewToShow: datePicker)  

        UIApplication.sharedApplication().cancelAllLocalNotifications()  
    }  
    else{  
        animateMyViews(datePicker, viewToShow: tblShoppingList)  

        scheduleLocalNotification()  
    }  

    txtAddItem.enabled = !txtAddItem.enabled  
}  


func numberOfSectionsInTableView(tableView: UITableView) -> Int {  
    return 1  
}  


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  
    var rows = 0  

    if let list = shoppingList{  
        rows = list.count  
    }  

    return rows  
}  


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {  
    let cell = tableView.dequeueReusableCellWithIdentifier("idCellItem")! as UITableViewCell  

    cell.textLabel?.text = shoppingList.objectAtIndex(indexPath.row) as! NSString as String  

    return cell  
}  


func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {  
    return 50.0  
}  

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {  
    if editingStyle == UITableViewCellEditingStyle.Delete {  
        removeItemAtIndex(indexPath.row)  
    }  
}  

func removeItemAtIndex(index: Int) {  
    shoppingList.removeObjectAtIndex(index)  

    tblShoppingList.reloadData()  

    saveShoppingList()  
}  



func saveShoppingList() {  
           let savePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("shopping_list")  

    shoppingList.writeToFile(savePath, atomically: true)  
}  

func loadShoppingList() {  
    let shoppingListPath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("shopping_list")  

    if NSFileManager.defaultManager().fileExistsAtPath(shoppingListPath){  
        shoppingList = NSMutableArray(contentsOfFile: shoppingListPath)  
        tblShoppingList.reloadData()  
    }  
}  

func animateMyViews(viewToHide: UIView, viewToShow: UIView) {  
    let animationDuration = 0.35  

    UIView.animateWithDuration(animationDuration, animations: { () -> Void in  
        viewToHide.transform = CGAffineTransformScale(viewToHide.transform, 0.001, 0.001)  
    }) { (completion) -> Void in  

        viewToHide.hidden = true  
        viewToShow.hidden = false  

        viewToShow.transform = CGAffineTransformScale(viewToShow.transform, 0.001, 0.001)  

        UIView.animateWithDuration(animationDuration, animations: { () -> Void in  
            viewToShow.transform = CGAffineTransformIdentity  
        })  
    }  
}  


}  
import UIKit

class ViewController: UIViewController {

var timer = NSTimer()
 var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    notificatioAppGoToBackground()

    setupNotificationSettings()

    //call handle notifications
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleModifyListNotification), name: "modifyListNotification", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleDeleteListNotification), name: "deleteListNotification", object: nil)

    backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
        UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!)
    })


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func alert(title:String, message:String ){

    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
    self.presentViewController(alert, animated: true){}
}

func handleModifyListNotification() {
    alert("Atenção", message: "Modificação")
}

func handleDeleteListNotification() {
    print("executou a exclusao")
}

func setupNotificationSettings() {

    let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()

    if (notificationSettings.types == UIUserNotificationType.None){

        // Specify the notification types.
        let notificationTypes = UIUserNotificationType.Alert.union(UIUserNotificationType.Sound)

        let modifyListAction = UIMutableUserNotificationAction()
        modifyListAction.identifier = "editList"
        modifyListAction.title = "Edit list"
        modifyListAction.activationMode = UIUserNotificationActivationMode.Foreground
        modifyListAction.destructive = false
        modifyListAction.authenticationRequired = true

        let trashAction = UIMutableUserNotificationAction()
        trashAction.identifier = "trashAction"
        trashAction.title = "Delete list"
        trashAction.activationMode = UIUserNotificationActivationMode.Background
        trashAction.destructive = true
        trashAction.authenticationRequired = true

        let actionsArray = NSArray(objects: modifyListAction, trashAction)
        let actionsArrayMinimal = NSArray(objects: trashAction, modifyListAction)

        // Specify the category related to the above actions.
        let shoppingListReminderCategory = UIMutableUserNotificationCategory()
        shoppingListReminderCategory.identifier = "shoppingListReminderCategory"
        shoppingListReminderCategory.setActions(actionsArray as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
        shoppingListReminderCategory.setActions(actionsArrayMinimal as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)


        let categoriesForSettings = NSSet(objects: shoppingListReminderCategory)


        // Register the notification settings.
        let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as? Set<UIUserNotificationCategory>)
        UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
    }

}

func setActions(actions: [AnyObject]!, forContext context: UIUserNotificationActionContext){}


func notificatioAppGoToBackground(){
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplicationWillResignActiveNotification, object: nil)
}

func appMovedToBackground() {
    print("App moved to background!!!")
    listenerSchedule()
}

func listenerSchedule() {
    timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: #selector(ViewController.startNotication), userInfo: nil, repeats: true)
}

func startNotication(){
    let date = NSDate()
    let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([.Day, .Month, .Year, .Hour, .Minute, .Minute], fromDate: date)


    print("aguardando notificação  \(dateComponets.second)" )
    let localNotification = UILocalNotification()
    //localNotification .fireDate = fixNotificationDate(datePicker.date) //usar com datepicker
    localNotification.alertBody = "Teste de notificação"
    localNotification.alertAction = "View List"
    localNotification.category = "shoppingListReminderCategory"

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
导入UIKit
类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource、UITextFieldDelegate{
@IBOutlet弱var txtAddItem:UITextField!
@IBOutlet弱var tblShoppingList:UITableView!
@IBOUTLE弱变差按钮:UIButton!
@IBVAR弱日期选择器:UIDatePicker!
var shoppingList:NSMutableArray!
var timer=NSTimer()
重写func viewDidLoad(){
super.viewDidLoad()
self.tblShoppingList.delegate=self
self.tblShoppingList.dataSource=self
self.txtAddItem.delegate=self
datePicker.hidden=true
loadShoppingList()
setupNotificationSettings()
NSNotificationCenter.defaultCenter().addObserver(self,选择器:#选择器(ViewController.handleModifyListNotification),名称:“modifyListNotification”,对象:nil)
NSNotificationCenter.defaultCenter().addObserver(self,选择器:#选择器(ViewController.handleDeleteListNotification),名称:“deleteListNotification”,对象:nil)
listenerSchedule()
}  
重写func didReceiveMemoryWarning(){
超级。我收到了记忆警告()
}  
func handleModifyListNotification(){
txtAddItem.becomeFirstResponder()
}  
func handleDeleteListNotification(){
shoppingList.removeAllObjects()
saveShoppingList()
tblShoppingList.reloadData()
}  
func setupNotificationSettings(){
让notificationSettings:UIUserNotificationSettings!=UIApplication.sharedApplication().currentUserNotificationSettings()
如果(notificationSettings.types==UIUserNotificationType.None){
让notificationTypes=UIUserNotificationType.Alert.union(UIUserNotificationType.Sound)
让justInformAction=UIMutableUserNotificationAction()
justInformAction.identifier=“justInform”
justInformAction.title=“好的,明白了”
justInformAction.activationMode=UIUserNotificationActivationMode.Background
justInformAction.destromical=false
justInformAction.authenticationRequired=false
让modifyListAction=UIMutableUserNotificationAction()
modifyListAction.identifier=“editList”
modifyListAction.title=“编辑列表”
modifyListAction.activationMode=UIUserNotificationActivationMode.前台
modifyListAction.destructive=false
modifyListAction.authenticationRequired=true
让TrasAction=UIMutableUserNotificationAction()
trashAction.identifier=“trashAction”
trashAction.title=“删除列表”
TrashaAction.activationMode=UIUserNotificationActivationMode.Background
trashAction.destromical=true
trashAction.authenticationRequired=true
let actionsArray=NSArray(对象:justInformAction、modifyListAction、TrashaAction)
let actionsArrayMinimal=NSArray(对象:trashAction,modifyListAction)
let ShoppingListMemberCategory=UIMutableUserNotificationCategory()
shoppingListReminderCategory.identifier=“shoppingListReminderCategory”
shoppingListReminderCategory.setActions(actionsArray为?[UIUserNotificationAction],forContext:UIUserNotificationActionContext.Default)
shoppingListReminderCategory.setActions(actionsArrayMinimal为?[UIUserNotificationAction],forContext:UIUserNotificationActionContext.Minimal)
let categoriesForSettings=NSSet(对象:shoppingListReminderCategory)
让newNotificationSettings=UIUserNotificationSettings(forTypes:notificationTypes,categories:categories for设置为?Set)
UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
}  
}  
func setActions(actions:[AnyObject]!,forContext上下文:UIUserNotificationActionContext){}
func scheduleLocalNotification(){
让localNotification=UILocalNotification()
localNotification.fireDate=fixNotificationDate(datePicker.date)
localNotification.alertBody=“测试”
localNotification.alertAction=“查看列表”
localNotification.category=“ShoppingListMemberCategory”
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}  
func listenerSchedule(){
timer=NSTimer.scheduledTimerWithTimeInterval(60.0,目标:self,选择器:#选择器(ViewController.startNotification),userInfo:nil,repeats:true)
}  
func startNotation(){
}  
func fixNotificationDate(dateToFix:NSDate)->NSDate{
让dateComponets:NSDateComponents=NSCalendar.currentCalendar().components([.Day、.Month、.Year、.Hour、.Minute],fromDate:dateToFix)
dateComponets.second=0
让fixedDate:NSDate!=NSCalendar.currentCalendar().dateFromComponents(DateComponents)
返回固定日期
}  
func textField应该返回(textField:UITextField)->Bool{
如果shoppingList==nil{
shoppingList=NSMutableArray()
}  
shoppingList.addObject(textField.text!)
tblShoppingList.reloadData()
txtAddItem.text=“”
txtAddItem.resignFirstResponder()辞职
saveShoppingList()
返回真值
}  
@iAction func scheduleReminder(发件人:AnyObject){
如果datePicker.hidden{
AnimateMyView(tblShoppingList,viewToShow:datePicker)
UIApplication.sharedApplication().cancelAllLocalNotifications()
}  
否则{
AnimateMyView(日期选择器、视图显示:tblShoppingList)
scheduleLocalNotification()
}  
txtAddItem.enabled=!txtAddItem.enabled
}  
func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回1
}  
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int
import UIKit

class ViewController: UIViewController {

var timer = NSTimer()
 var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    notificatioAppGoToBackground()

    setupNotificationSettings()

    //call handle notifications
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleModifyListNotification), name: "modifyListNotification", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleDeleteListNotification), name: "deleteListNotification", object: nil)

    backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
        UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!)
    })


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func alert(title:String, message:String ){

    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
    self.presentViewController(alert, animated: true){}
}

func handleModifyListNotification() {
    alert("Atenção", message: "Modificação")
}

func handleDeleteListNotification() {
    print("executou a exclusao")
}

func setupNotificationSettings() {

    let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()

    if (notificationSettings.types == UIUserNotificationType.None){

        // Specify the notification types.
        let notificationTypes = UIUserNotificationType.Alert.union(UIUserNotificationType.Sound)

        let modifyListAction = UIMutableUserNotificationAction()
        modifyListAction.identifier = "editList"
        modifyListAction.title = "Edit list"
        modifyListAction.activationMode = UIUserNotificationActivationMode.Foreground
        modifyListAction.destructive = false
        modifyListAction.authenticationRequired = true

        let trashAction = UIMutableUserNotificationAction()
        trashAction.identifier = "trashAction"
        trashAction.title = "Delete list"
        trashAction.activationMode = UIUserNotificationActivationMode.Background
        trashAction.destructive = true
        trashAction.authenticationRequired = true

        let actionsArray = NSArray(objects: modifyListAction, trashAction)
        let actionsArrayMinimal = NSArray(objects: trashAction, modifyListAction)

        // Specify the category related to the above actions.
        let shoppingListReminderCategory = UIMutableUserNotificationCategory()
        shoppingListReminderCategory.identifier = "shoppingListReminderCategory"
        shoppingListReminderCategory.setActions(actionsArray as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
        shoppingListReminderCategory.setActions(actionsArrayMinimal as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)


        let categoriesForSettings = NSSet(objects: shoppingListReminderCategory)


        // Register the notification settings.
        let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as? Set<UIUserNotificationCategory>)
        UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
    }

}

func setActions(actions: [AnyObject]!, forContext context: UIUserNotificationActionContext){}


func notificatioAppGoToBackground(){
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplicationWillResignActiveNotification, object: nil)
}

func appMovedToBackground() {
    print("App moved to background!!!")
    listenerSchedule()
}

func listenerSchedule() {
    timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: #selector(ViewController.startNotication), userInfo: nil, repeats: true)
}

func startNotication(){
    let date = NSDate()
    let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([.Day, .Month, .Year, .Hour, .Minute, .Minute], fromDate: date)


    print("aguardando notificação  \(dateComponets.second)" )
    let localNotification = UILocalNotification()
    //localNotification .fireDate = fixNotificationDate(datePicker.date) //usar com datepicker
    localNotification.alertBody = "Teste de notificação"
    localNotification.alertAction = "View List"
    localNotification.category = "shoppingListReminderCategory"

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}