如何在特定时间每天在特定时间触发本地通知3
我是iOS开发新手,但已经创建了该应用程序,并且正在尝试为特定时间(例如(10AM))创建每日通知。现在,如果我的移动设备时间到达上午10点,通知就会变得太多。我想在给定的特定时间点上午10点触发本地通知,我想每天重复一次。每天重复通知的最佳方法是什么?如何在特定时间每天在特定时间触发本地通知3
这里是我的代码
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)
}
你的问题是消防日这是不正确的
您可以创建火日期一样,如果你想验证火灾日期这样
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)
我希望这会帮助你。
如何每天重复? –
更新我的回答 –
让今天= NSDate的() 让日历= NSCalendar.currentCalendar() 让组件= calendar.components([日,.Month,.Year。] FROM日期:今天) 让dateComp:NSDateComponents = NSDateComponents() dateComp.day = components.day dateComp.month = components.month dateComp.year = components.year dateComp.hour = 10 dateComp.minute = 00 令日期= calendar.dateFromComponents(dateComp)我已经改变了这样的代码bez你的代码dosent支持swift3,现在得到3通知,如果我把我的设备设置为10AM –
在夫特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)
嘿,我不能够在特定的时间开火通知。我做了相同的代码。 – Abha
的可能的复制[在设定的时间以快速重复每日本地通知(http://stackoverflow.com/questions/30619998/repeating-local-通知 - 每天一个 - 设置 - 与 - 迅速) – sanman