如何通过按下 swift 中的按钮取消 localNotification?
how to cancel a localNotification with the press of a button in swift?
我正在通过单击按钮安排基于 UILocalNotification
的位置。但是当我尝试通过再次单击同一个按钮来取消 localNotification 时,它不会取消通知。
我正在使用 UIApplication.sharedApplication().cancelLocalNotification(localNotification)
取消我预定的基于位置的本地通知。
我究竟做错了什么 ?
这是我的实现
@IBAction func setNotification(sender: UIButton!) {
if sender.tag == 999 {
sender.setImage(UIImage(named: "NotificationFilled")!, forState: .Normal)
sender.tag = 0
regionMonitor() //function where notification get scheduled
} else {
sender.setImage(UIImage(named: "Notification")!, forState: .Normal)
sender.tag = 999 }
我应该在 else 块中输入什么,以便取消预定的通知。无法清除所有通知。
这是我触发本地通知的 didEnterRegion
代码块
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
localNotification.regionTriggersOnce = true
localNotification.alertBody = "Stack Overflow is great"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
NSLog("Entering region")
}
不看代码的实现很难说。
所以你有一个
- IBAction methodName{
//scheduleNotification
//cancelNotification
}
差不多吧?如果是这样添加一个 bool so
Bool didCancel;
- IBAction methodName{
if didCancel == No{
//scheduleNotification
didCancel = YES;
}else if didCancel == YES{
//cancelNotifications
didCancel = NO;
}
如果您的情况可以接受,您可以尝试删除所有通知。
像这样:
for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
或如 Logan 所述:
UIApplication.sharedApplication().cancelAllLocalNotifications()
或者如 Gerard Grundy 所说 Swift 4:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
您可以在本地通知的用户信息中为键保存一个唯一值,并通过使用该键的值获取本地通知来取消它。
试试这个(Objective C):
NSArray *notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i=0; i<[notifArray count]; i++)
{
UILocalNotification* notif = [notifArray objectAtIndex:i];
NSDictionary *userInfoDict = notif.userInfo;
NSString *uniqueKeyVal=[NSString stringWithFormat:@"%@",[userInfoDict valueForKey:@"UniqueKey"]];
if ([uniqueKeyVal isEqualToString:keyValToDelete])
{
[[UIApplication sharedApplication] cancelLocalNotification:notif];
break;
}
}
iOS10+Swift3.1
的解决方案
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()
对于 Swift 3.0 和 iOS 10:
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
您可以使用其标识符取消通知:
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [String])
center.removePendingNotificationRequests(withIdentifiers: [String])
我正在通过单击按钮安排基于 UILocalNotification
的位置。但是当我尝试通过再次单击同一个按钮来取消 localNotification 时,它不会取消通知。
我正在使用 UIApplication.sharedApplication().cancelLocalNotification(localNotification)
取消我预定的基于位置的本地通知。
我究竟做错了什么 ?
这是我的实现
@IBAction func setNotification(sender: UIButton!) {
if sender.tag == 999 {
sender.setImage(UIImage(named: "NotificationFilled")!, forState: .Normal)
sender.tag = 0
regionMonitor() //function where notification get scheduled
} else {
sender.setImage(UIImage(named: "Notification")!, forState: .Normal)
sender.tag = 999 }
我应该在 else 块中输入什么,以便取消预定的通知。无法清除所有通知。
这是我触发本地通知的 didEnterRegion
代码块
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
localNotification.regionTriggersOnce = true
localNotification.alertBody = "Stack Overflow is great"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
NSLog("Entering region")
}
不看代码的实现很难说。
所以你有一个
- IBAction methodName{
//scheduleNotification
//cancelNotification
}
差不多吧?如果是这样添加一个 bool so
Bool didCancel;
- IBAction methodName{
if didCancel == No{
//scheduleNotification
didCancel = YES;
}else if didCancel == YES{
//cancelNotifications
didCancel = NO;
}
如果您的情况可以接受,您可以尝试删除所有通知。 像这样:
for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
或如 Logan 所述:
UIApplication.sharedApplication().cancelAllLocalNotifications()
或者如 Gerard Grundy 所说 Swift 4:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
您可以在本地通知的用户信息中为键保存一个唯一值,并通过使用该键的值获取本地通知来取消它。 试试这个(Objective C):
NSArray *notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i=0; i<[notifArray count]; i++)
{
UILocalNotification* notif = [notifArray objectAtIndex:i];
NSDictionary *userInfoDict = notif.userInfo;
NSString *uniqueKeyVal=[NSString stringWithFormat:@"%@",[userInfoDict valueForKey:@"UniqueKey"]];
if ([uniqueKeyVal isEqualToString:keyValToDelete])
{
[[UIApplication sharedApplication] cancelLocalNotification:notif];
break;
}
}
iOS10+Swift3.1
的解决方案let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()
对于 Swift 3.0 和 iOS 10:
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
您可以使用其标识符取消通知:
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [String])
center.removePendingNotificationRequests(withIdentifiers: [String])