作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 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)
}
最佳答案
在 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)
关于ios - 如何在swift 3中每天在特定时间触发本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40520704/
我是一名优秀的程序员,十分优秀!