1

I am new to iOS development, but have created the app and I am trying to create a daily notification for specific time like (10AM). Now notification are coming too many if i se my mobile device time to 10AM. I want to trigger a local notification only once at given specific time that is at 10 AM , and I want to repeat this daily. What is the best method to repeat the notification daily ?

Here is my code

func fire(){ let dateComp:NSDateComponents = NSDateComponents() dateComp.hour = 10 dateComp.minute = 00 dateComp.timeZone = NSTimeZone.systemTimeZone() let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierIndian)! let date:NSDate = calender.dateFromComponents(dateComp)! let localNotificationSilent = UILocalNotification() localNotificationSilent.fireDate = date localNotificationSilent.repeatInterval = NSCalendarUnit.Day localNotificationSilent.alertBody = "Started!" localNotificationSilent.alertAction = "swipe to hear!" localNotificationSilent.timeZone = NSCalendar.currentCalendar().timeZone localNotificationSilent.category = "PLAY_CATEGORY" UIApplication.sharedApplication().scheduleLocalNotification(localNotificationSilent) } 
1

2 Answers 2

3

Working in Swift 3, iOS 10:

UNUserNotificationCenter.current().requestAuthorization( options: [.alert, .sound, .badge], completionHandler: {userDidAllow, error in //if userDidAllow : do something if you want to }) //Set notification to trigger 11:30AM everyday var dateComponents = DateComponents() dateComponents.hour = 11 dateComponents.minute = 30 let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) //Set your content let content = UNMutableNotificationContent() content.title = "Your notification title" content.body = "Your notification body" let request = UNNotificationRequest( identifier: "yourIdentifier", content: content, trigger: trigger ) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, I am not able to fire notification at specific time. I did same code.
2

your problem is fire date which is not proper

you can create fire date like this way

 let today = Date() let calendar = NSCalendar.current let components = calendar.dateComponents([.day, .month, .year], from: today) var dateComp:DateComponents = DateComponents() dateComp.day = components.day dateComp.month = components.month dateComp.year = components.year dateComp.hour = 10 dateComp.minute = 00 let date = calendar.date(from: dateComp) 

if you want to verify fire date then you can check like this way

 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd MM yyyy hh:mm:ss" let fireDate = dateFormatter.string(from: date!) print("fireDate: \(fireDate)") 

Now it's time to set fire date to local notification

localNotificationSilent.fireDate = date // no need to set time zone Remove bellow line localNotificationSilent.timeZone = NSCalendar.currentCalendar().timeZone 

Notification code

 let localNotificationSilent = UILocalNotification() localNotificationSilent.fireDate = date localNotificationSilent.repeatInterval = .day localNotificationSilent.alertBody = "Started!" localNotificationSilent.alertAction = "swipe to hear!" localNotificationSilent.category = "PLAY_CATEGORY" UIApplication.shared.scheduleLocalNotification(localNotificationSilent) 

I hope it will help you.

4 Comments

how to repeat it daily??
let today = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day, .Month, .Year], fromDate: today) let dateComp:NSDateComponents = NSDateComponents() dateComp.day = components.day dateComp.month = components.month dateComp.year = components.year dateComp.hour = 10 dateComp.minute = 00 let date = calendar.dateFromComponents(dateComp) i have changed code like this bez ur code dosent support swift3 and now getting 3 notification if i set my device to 10AM
@jigneshVadadoriya how to restrict notification on Saturday and Sunday from above code. I mean don't want to fire on sat and Sunday.
@User558 you need to change in above code like localNotificationSilent.repeatInterval = .weekday OR localNotificationSilent.repeatInterval = .weekdayOrdinal May br it will help you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.