Ios 如何在swift中取消特定的UILocalNotification?
我在表视图中有一个单元格,其中有一个用于安排通知的按钮。是否可以删除或取消该特定单元格的通知,而不影响其他单元格安排的通知?我想使用相同的按钮删除或取消特定通知。 如果是这样,请帮我提供一个swift代码示例Ios 如何在swift中取消特定的UILocalNotification?,ios,iphone,swift,uitableview,uilocalnotification,Ios,Iphone,Swift,Uitableview,Uilocalnotification,我在表视图中有一个单元格,其中有一个用于安排通知的按钮。是否可以删除或取消该特定单元格的通知,而不影响其他单元格安排的通知?我想使用相同的按钮删除或取消特定通知。 如果是这样,请帮我提供一个swift代码示例 @IBAction func setNotification(sender: UIButton!) { cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)
@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 } }
didEnterRegion()的代码
您可以在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
}
}
}
}
更新至Swift 3,并进行了改进,以防止nil
崩溃您可以使用它保存设置
要启用或禁用didSelectRowAtIndexPath方法中的设置,请执行以下操作:
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "ShouldShowRegionEnteredNotification") //true or false
然后您可以检查设置,如下所示:
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")
}
}
你能添加你用来安排通知的代码吗?@ZeMoon我已经更新了我的问题让我把这个弄对。。。您正在安排即时通知,对吗?@ZeMoon我正在安排基于位置的通知。当用户进入该区域时,将触发通知。我想,如果用户出于某种原因改变了对通知的想法并再次单击按钮取消通知,那么如果他进入该地区,则不会触发通知。如果我的回答对您有帮助,请进行投票并接受。谢谢很抱歉迟了答复。如何为userInfo
中的每个UILocalNotification
保存唯一标识符?我想到了类似于let userInfo:NSDictionary=NSDictionary(object:what go here?,forKey:what go here?)localNotification.userInfo=userInfo as[NSObject:AnyObject]
对象和键的内部是什么?这是我在UIApplication.sharedApplication().scheduledLocalNotifications中取消通知的代码!{if(notification.userInfo![“这里是什么?”]as!String!==这里是什么?{UIApplication.sharedApplication().cancelocalnotification(notification)break}
让userInfo:NSDictionary=NSDictionary(对象:“your-unique-id-12345”如果我的回答对你有帮助,请投票并接受它。谢谢
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")
}
}