如何在swift中取消特定的UILocalNotification?

时间:2022-06-01 21:03:21

I have a cell in a table view which has a button for scheduling notifications. Is it possible to delete or cancel a notification for that particular cell without affecting the notifications scheduled by other cells ? I want to delete or cancel the particular notification using the same button. If so, please help me with a sample swift code.

我在表视图中有一个单元格,其中有一个用于安排通知的按钮。是否可以删除或取消该特定单元格的通知而不影响其他单元格安排的通知?我想使用相同的按钮删除或取消特定通知。如果是这样,请帮我提供一个示例快速代码。

@IBAction func setNotification(sender: UIButton!) {

        cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! CustomTableViewCell
        if sender.selected {

            sender.selected = false
             }else {
currentRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: latitude ,longitude:longitude), radius: radius, identifier: identifier)
regionMonitor()
            manager.stopUpdatingLocation()
            sender.selected = true } }

code for didEnterRegion()

didEnterRegion()的代码

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
            localNotification.regionTriggersOnce = true
            localNotification.alertBody = "you have reached"
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
            NSLog("Entering region") }

2 个解决方案

#1


10  

You could save a unique identifier for each UILocalNotification in the userInfo and then loop through them all to find the unique id and delete it:

您可以在userInfo中为每个UILocalNotification保存唯一标识符,然后循环遍历它们以查找唯一ID并将其删除:

var app:UIApplication = UIApplication.shared
    if let localNotifications = app.scheduledLocalNotifications {
        for oneEvent in localNotifications {
            var notification = oneEvent as UILocalNotification
            if let userInfoCurrent = notification.userInfo as? [String:AnyObject],
                let uid = userInfoCurrent["uid"] as? String {
                if uid == uidtodelete {
                    //Cancelling local notification
                    app.cancelLocalNotification(notification)
                    break
                }
            }
        }
    }

Updated to Swift 3 and improved to prevent nil crashes

更新到Swift 3并进行了改进以防止无崩溃

#2


1  

You can use NSUserDefaults to save the setting.

您可以使用NSUserDefaults保存设置。

To enable or disable the setting in the didSelectRowAtIndexPath method:

要在didSelectRowAtIndexPath方法中启用或禁用该设置:

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "ShouldShowRegionEnteredNotification") //true or false

Then you can check the setting like so:

然后你可以这样检查设置:

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {

    if (NSUserDefaults.standardUserDefaults().boolForKey("ShouldShowRegionEnteredNotification"))
    {
        localNotification.regionTriggersOnce = true
        localNotification.alertBody = "you have reached"
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
        NSLog("Entering region")
    }
}

#1


10  

You could save a unique identifier for each UILocalNotification in the userInfo and then loop through them all to find the unique id and delete it:

您可以在userInfo中为每个UILocalNotification保存唯一标识符,然后循环遍历它们以查找唯一ID并将其删除:

var app:UIApplication = UIApplication.shared
    if let localNotifications = app.scheduledLocalNotifications {
        for oneEvent in localNotifications {
            var notification = oneEvent as UILocalNotification
            if let userInfoCurrent = notification.userInfo as? [String:AnyObject],
                let uid = userInfoCurrent["uid"] as? String {
                if uid == uidtodelete {
                    //Cancelling local notification
                    app.cancelLocalNotification(notification)
                    break
                }
            }
        }
    }

Updated to Swift 3 and improved to prevent nil crashes

更新到Swift 3并进行了改进以防止无崩溃

#2


1  

You can use NSUserDefaults to save the setting.

您可以使用NSUserDefaults保存设置。

To enable or disable the setting in the didSelectRowAtIndexPath method:

要在didSelectRowAtIndexPath方法中启用或禁用该设置:

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "ShouldShowRegionEnteredNotification") //true or false

Then you can check the setting like so:

然后你可以这样检查设置:

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {

    if (NSUserDefaults.standardUserDefaults().boolForKey("ShouldShowRegionEnteredNotification"))
    {
        localNotification.regionTriggersOnce = true
        localNotification.alertBody = "you have reached"
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
        NSLog("Entering region")
    }
}