Swift- 在用户选择的时间推送通知
Swift- Pushing notifications at a user selected time
如何让用户使用日期选择器选择时间,然后向用户发送本地通知?
这是一个简单的代码,用于在 swift 中安排本地通知:
let calendar: NSCalendar = NSCalendar.currentCalendar()
let fireDateOfNotification: NSDate = //The date which was picked from the picker
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.localTimeZone()
notification.alertBody = "ALERT STRING"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = fireDateOfNotification
notification.userInfo = ["UUID": "NotificationID"] //id for the local notification in case you want to cancel it
UIApplication.sharedApplication().scheduleLocalNotification(notification)
如果您想取消本地通知,您应该使用以下功能:
func cancelLocalNotificationsWithUUID(uuid: Int) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
let notification = item as! UILocalNotification
if let notificationUUID = notification.userInfo?["UUID"] as? Int {
if notificationUUID == uuid {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
希望对您有所帮助。
如何让用户使用日期选择器选择时间,然后向用户发送本地通知?
这是一个简单的代码,用于在 swift 中安排本地通知:
let calendar: NSCalendar = NSCalendar.currentCalendar()
let fireDateOfNotification: NSDate = //The date which was picked from the picker
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.localTimeZone()
notification.alertBody = "ALERT STRING"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = fireDateOfNotification
notification.userInfo = ["UUID": "NotificationID"] //id for the local notification in case you want to cancel it
UIApplication.sharedApplication().scheduleLocalNotification(notification)
如果您想取消本地通知,您应该使用以下功能:
func cancelLocalNotificationsWithUUID(uuid: Int) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
let notification = item as! UILocalNotification
if let notificationUUID = notification.userInfo?["UUID"] as? Int {
if notificationUUID == uuid {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
希望对您有所帮助。