如何在Swift中通过按一个按钮取消本地通知?

22

我正在使用一个按钮来安排基于位置的UILocalNotification。但是,当我尝试通过再次单击相同的按钮来取消本地通知时,它没有被取消。 我正在使用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")
}
6个回答

58

如果在您的场景中可以接受,您可以尝试移除所有通知。就像这样:

for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] { 
  UIApplication.sharedApplication().cancelLocalNotification(notification)
}

或者按照Logan所述:

UIApplication.sharedApplication().cancelAllLocalNotifications()

根据Gerard Grundy在Swift 4中的说法:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

12
如果您无论如何都要取消所有通知,可以直接使用以下代码:UIApplication.sharedApplication().cancelAllLocalNotifications() - Logan
它取消了本地通知,但由于自定义声音持续播放约3到4秒钟,因此未取消自定义声音。 - Gautam Sareriya
2
Swift4.1 更新日志和 → UIApplication.shared.cancelAllLocalNotifications() - uplearned.com
Swift 5 更新:UNUserNotificationCenter.current().removeAllPendingNotificationRequests()。由于某种原因,如果您将相同的文本粘贴到 Swift 5 中,它会出现错误。您只需删除最后一部分“removeAllPendingNotificationRequests()”,然后重新输入即可修复错误。(可能是一个 bug) - uplearned.com
UIApplication.shared.cancelAllLocalNotifications()在iOS 10.0中已被弃用。我们需要使用用户通知框架。 - Arun Reddy

8
您可以使用其标识符取消通知:
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [String])
center.removePendingNotificationRequests(withIdentifiers: [String])

4

iOS 10+ Swift 3.1解决方案

let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()

1

对于Swift 3.0和iOS 10:

UNUserNotificationCenter.current().removeAllDeliveredNotifications()

1
您可以在本地通知的用户信息中保存键的唯一值,并通过使用该键的值获取本地通知来取消它。 尝试以下代码(用于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;
    }
}

-4
很难在没有看到代码实现的情况下进行判断。
所以你有一个


- IBAction methodName{

//scheduleNotification
//cancelNotification
}

这样对吗?如果是的话,添加一个布尔值

Bool didCancel;

- IBAction methodName{

if didCancel == No{
//scheduleNotification
didCancel = YES;
}else if didCancel == YES{
//cancelNotifications
didCancel = NO;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接