ios - NSLocalNotification's repeating. Dismissing. Proper way -
i want create daily notification @ 10am different content every day.
func createnotification() { let datecomp:nsdatecomponents = nsdatecomponents() datecomp.hour = 10; // supposed run each day @ 10am var mycalendar:nscalendar = nscalendar(calendaridentifier: nscalendaridentifiergregorian)! var date:nsdate = mycalendar.datefromcomponents(datecomp)! let localnotification = uilocalnotification() localnotification.firedate = date localnotification.alertbody = getcontentstringfornotification() // method returns different string each date localnotification.repeatinterval = .day // repeats every day localnotification.timezone = nstimezone.defaulttimezone() uiapplication.sharedapplication().schedulelocalnotification(localnotification) }
it duplicates notifications. keep repeating , returning yesterdays notifications. searched through stack community questions, did not find proper way dismiss them.
as understand:
- the app terminated , scheduled , act independent of app. repeatedly running app create more notifications.
- notifications not dismissed , return yesterdays message next day.
help with:
- what's proper way dismiss them or check if have been fired (making sure don't repeat
- do loop , create notifications year forth each day or create new 1 tomorrow each time app run
what missing? code welcomed. thank you!
you're scheduling 1 notification in code, single message, shown user once per day. if run code each time open app, or each day, you'll result in lot of different notifications registered, each firing once per day , (possibly different) message.
you should using cancelalllocalnotifications
or cancellocalnotification:
remove old notifications.
likely don't want set repeat interval. instead, each time app opened schedule 7 notifications on subsequent days next week. if user hasn't used app in 7 days should stop bothering them notifications (though can register more app limit).
Comments
Post a Comment