Ios 取消并重新安排本地通知(Swift)
我试图显示一个本地通知,时间间隔为1天(24小时),工作正常,但现在我想取消特定日期的通知。我(今天)试过,但似乎不起作用。我的通知仍在出现这是我为取消通知所做的:Ios 取消并重新安排本地通知(Swift),ios,swift2,uilocalnotification,Ios,Swift2,Uilocalnotification,我试图显示一个本地通知,时间间隔为1天(24小时),工作正常,但现在我想取消特定日期的通知。我(今天)试过,但似乎不起作用。我的通知仍在出现这是我为取消通知所做的: let appNotification = appDelegate!.notification UIApplication.sharedApplication().cancelLocalNotification(appNotification) 这是我在appDelegate中安排通知的方式: //scheduling noti
let appNotification = appDelegate!.notification
UIApplication.sharedApplication().cancelLocalNotification(appNotification)
这是我在appDelegate
中安排通知的方式:
//scheduling notification
dateComp.year = 2015;
dateComp.month = 01;
dateComp.day = 20;
dateComp.hour = 21;
dateComp.minute = 05;
dateComp.timeZone = NSTimeZone.systemTimeZone()
notification.category = "FIRST_CATEGORY"
notification.alertBody = "my daily notiification ?"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = finalFireDate
notification.repeatInterval = NSCalendarUnit.Day
let dateNow = NSDate()
let noticalendar = NSCalendar.currentCalendar()
let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
我不知道为什么无法取消通知。如果有任何内容丢失或出错,请告诉我。尝试此代码
var app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications! {
var notification = oneEvent as UILocalNotification
if notification.fireDate == "your date" {
//Cancelling local notification
app.cancelLocalNotification(notification)
break;
}
}
试试这个代码
var app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications! {
var notification = oneEvent as UILocalNotification
if notification.fireDate == "your date" {
//Cancelling local notification
app.cancelLocalNotification(notification)
break;
}
}
您可以使用通知的
userInfo
属性
例如,如果您按如下方式安排:
notificationID = 1
notification.category = "FIRST_CATEGORY"
// other settings...
notification.userInfo = ["NotificationID": notificationID]
您可以使用userInfo
查找它:
let application = UIApplication.sharedApplication()
let scheduledNotifications = application.scheduledLocalNotifications!
for notification in scheduledNotifications {
if let nID = notification.userInfo?["NotificationID"] as? Int {
if nID == appDelegate!.notificationID {
application.cancelLocalNotification(notification)
break
}
}
}
您可以使用通知的
userInfo
属性
例如,如果您按如下方式安排:
notificationID = 1
notification.category = "FIRST_CATEGORY"
// other settings...
notification.userInfo = ["NotificationID": notificationID]
您可以使用userInfo
查找它:
let application = UIApplication.sharedApplication()
let scheduledNotifications = application.scheduledLocalNotifications!
for notification in scheduledNotifications {
if let nID = notification.userInfo?["NotificationID"] as? Int {
if nID == appDelegate!.notificationID {
application.cancelLocalNotification(notification)
break
}
}
}
谢谢回复:),但问题是我没有太多的通知,我有一个通知每天都在重复,所以fireDate是一个相同的日期,因此,使用您的解决方案将无法纠正我的错误@MadhumithaIts取消本地通知,但不会取消自定义声音,因为它将持续播放约3到4秒。感谢您的回复:),但问题是我没有太多的通知,我有一个通知,它每天都在重复,所以fireDate是一个相同的日期,因此,使用您的解决方案将无法纠正我的错误@MadhumithaIts取消本地通知,但不会取消自定义声音,因为它会持续播放3到4秒左右。