swift - How to put notifications on iOS application to repeat every one (1) hour? -
i tried put notifications in app, supposed repeat every 1 hour repeat unregulated, clear, repeats 30min 1 hour long time etc.. code used in "appdelegate.swift":
class appdelegate: uiresponder, uiapplicationdelegate { var window: uiwindow? func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { // override point customization after application launch. //notification repeat application.registerusernotificationsettings(uiusernotificationsettings(fortypes: uiusernotificationtype.alert | uiusernotificationtype.badge | uiusernotificationtype.sound, categories: nil)) return true }
and code used in "viewcontroller.swift":
//notification repeat var time = 1 override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. //notification repeat var timer = nstimer.scheduledtimerwithtimeinterval(3600.0, target: self, selector: selector("activatenotifications"), userinfo: nil, repeats: true) } //notification repeat func activatenotifications() { time -= 1 if (time <= 0){ var activatenotifications = uilocalnotification() activatenotifications.alertaction = “hey" activatenotifications.alertbody = “hello world!" activatenotifications.firedate = nsdate(timeintervalsincenow: 0) uiapplication.sharedapplication().schedulelocalnotification(activatenotifications) } }
can me, made mistake ?
you don't need timer @ all. uilocalnotification
class has property entitled repeatinterval
that, can expect, set interval @ notification repeated.
according this, can schedule local notifications repeated every hour in following way:
func viewdidload() { super.viewdidload() var notification = uilocalnotification() notification.alertbody = "..." // text displayed in notification notification.firedate = nsdate() // right (when notification fired) notification.soundname = uilocalnotificationdefaultsoundname // play default sound notification.repeatinterval = nscalendarunit.calendarunithour // line defines interval @ notification repeated uiapplication.sharedapplication().schedulelocalnotification(notification) }
note: sure execute code when launch notification once, since schedules different notification every time executed. better understanding of local notifications, can read local notifications in ios 8 swift (part 1) , local notifications in ios 8 swift (part 2).
Comments
Post a Comment