Reputation: 291
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) {
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.alertBody = "quote of the day"
notifications.category = "First_category"
UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
application.applicationIconBadgeNumber = 0
}
Upvotes: 1
Views: 507
Reputation: 9540
I can help you with your two times Local notification problem.
Solution:
The problem is that when you add any local notification - First of all you need to check that if any local notification is scheduled or not?
So, how we can check?
Well, you need to store some data for that in notifications.userInfo
.
E.g.
var userInfo = [String:String]()
let example = "SomeNotification"
userInfo["example"] = example
notification.userInfo = userInfo
Reference: https://www.hackingwithswift.com/read/21/2/scheduling-notifications-uilocalnotification
So, once you update the time for the same notification, you need to cancel the existing notification and set the new notification with new time.
var app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications {
var notification = oneEvent as UILocalNotification
let userInfoCurrent = notification.userInfo! as [String:AnyObject]
let uid = userInfoCurrent["uid"]! as String
if uid == uidtodelete {
//Cancelling local notification
app.cancelLocalNotification(notification)
break;
}
}
Reference: Delete a particular local notification
Upvotes: 2