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)    
}
Raj stephen
  • 27
  • 1
  • 2
  • 5
  • Possible duplicate of [Repeating local notification daily at a set time with swift](http://stackoverflow.com/questions/30619998/repeating-local-notification-daily-at-a-set-time-with-swift) – Sanman Nov 10 '16 at 05:48

2 Answers2

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)
Erik Nguyen
  • 346
  • 2
  • 6
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.

jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
  • how to repeat it daily?? – Raj stephen Nov 10 '16 at 06:50
  • 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 – Raj stephen Nov 10 '16 at 07:47
  • @jigneshVadadoriya how to restrict notification on Saturday and Sunday from above code. I mean don't want to fire on sat and Sunday. – User558 Nov 08 '17 at 07:01
  • @User558 you need to change in above code like localNotificationSilent.repeatInterval = .weekday OR localNotificationSilent.repeatInterval = .weekdayOrdinal May br it will help you – jignesh Vadadoriya Nov 08 '17 at 14:24