swift2 - About notification iOS -
i'm studying notifications, , create code thar show notification using datepicker schedule. code worked well. how show notification each 10 minutes when app in background? os code:
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() }
viewcontroller:
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")! uitableviewcell cell.textlabel?.text = shoppinglist.objectatindex(indexpath.row) as! nsstring 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() nsstring).stringbyappendingpathcomponent("shopping_list") shoppinglist.writetofile(savepath, atomically: true) } func loadshoppinglist() { let shoppinglistpath = (nstemporarydirectory() 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 }) } } }
once app killed, can't exec code, because there not process active in ios (you can have service instead in android). can try study this apple guide background operation (i think dropbox app uses kind of background operations, quite slow when uploading images)
Comments
Post a Comment