如何在swift 3中每天在特定时间触发本地通知

时间:2016-11-10 05:45:47

标签: ios iphone swift uilocalnotification nscalendar

我是iOS开发的新手,但已经创建了应用程序,我正在尝试创建特定时间的每日通知,如(上午10点)。如果我的移动设备时间到上午10点,现在通知太多了。我想在给定的特定时间(上午10点)触发一次本地通知,我想每天重复一次。每天重复通知的最佳方法是什么?

这是我的代码

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)    
}

2 个答案:

答案 0 :(得分:1)

你的问题是火灾日期不合适

你可以像这样创建开火日期

    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)

如果您想验证开火日期,那么您可以这样检查

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

现在是时候将开火日期设置为本地通知

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

通知代码

    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)

我希望它会对你有所帮助。

答案 1 :(得分:1)

在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)