Reputation: 123
I am using localNotifications for my app.I have a datepicker from which a users picks date and time for the notifications and I also have 3 segmented controls(daily,weekly,monthly).I have done the coding for localnotification and its working.But the segmented control is not working and one more problem if a person chooses two times like one for(1 pm) and second for (2pm) and then the notifications are displaying in both time which i dont want.Below is the code
In appdelegate.swift
let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let myAlarmSetting:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as Set<NSObject>)
UIApplication.sharedApplication().registerUserNotificationSettings(myAlarmSetting)
In notifications.swift
@IBAction func NotificationButtonTapped(sender: AnyObject) {
func cancelLocalNotificationsWithUUID(uuid: String) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
let notification = item as! UILocalNotification
if let notificationUUID = notification.userInfo?["UUID"] as? String {
if notificationUUID == uuid {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
var notifications:UILocalNotification = UILocalNotification()
notifications.fireDate = datePicker.date
notifications.timeZone = NSTimeZone.defaultTimeZone()
notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
notifications.soundName = UILocalNotificationDefaultSoundName
notifications.userInfo = ["UUID": "YOUR NOTIFICATION ID"]
switch(frequencysegmentedcontrol.selectedSegmentIndex){
case 0:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay
break;
case 1:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear
break;
case 2:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitYear
break;
default:
notifications.repeatInterval = NSCalendarUnit(0)
break;
}
notifications.alertBody = "quote of the day"
notifications.category = "First_category"
UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
application.applicationIconBadgeNumber = 0
}
Upvotes: 2
Views: 97
Reputation: 1752
that's happening because you are not canceling the old notification when you schedule a new one, you can always add info to your notification like this:
var notification = UILocalNotification()
...
notification.userInfo = ["UUID": "YOUR NOTIFICATION ID"]
and whenever you schedule a new notification you should cancel old notifications like this:
func cancelLocalNotificationsWithUUID(uuid: String) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
let notification = item as! UILocalNotification
if let notificationUUID = notification.userInfo?["UUID"] as? String {
if notificationUUID == uuid {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
Here is the full code:
func cancelLocalNotificationsWithUUID(uuid: String) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
let notification = item as! UILocalNotification
if let notificationUUID = notification.userInfo?["UUID"] as? String {
if notificationUUID == uuid {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
@IBAction func NotificationButtonTapped(sender: AnyObject) {
cancelLocalNotificationsWithUUID("NotificationID")
var notifications:UILocalNotification = UILocalNotification()
notifications.fireDate = datePicker.date
notifications.timeZone = NSTimeZone.defaultTimeZone()
notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
notifications.soundName = UILocalNotificationDefaultSoundName
switch(frequencysegmentedcontrol.selectedSegmentIndex){
case 0:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay
break;
case 1:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear
break;
case 2:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitYear
break;
default:
notifications.repeatInterval = NSCalendarUnit(0)
break;
}
notifications.userInfo = ["UUID": "NotificationID"]
notifications.alertBody = "quote of the day"
notifications.category = "First_category"
UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
application.applicationIconBadgeNumber = 0
}
Upvotes: 2