我正在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 }
Run Code Online (Sandbox Code Playgroud)
我该怎么进入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")
}
Run Code Online (Sandbox Code Playgroud) 因为UILocalNotification现在已弃用,所以我将代码移至新UNNotificationRequestAPI。
它指出:' cancelLocalNotification ' 在 iOS 10.0 中被弃用:使用 UserNotifications Framework's-[UNUserNotificationCenter removePendingNotificationRequestsWithIdentifiers:]
但它似乎并不相等-虽然我可以随时删除消息cancelLocalNotification(即使它们已显示/已发送),但似乎removePendingNotificationRequestsWithIdentifiers只能删除未发送的通知。这真的很烦人。
所以我的问题是:是否有一种有效的方法可以删除排队的通知,UNNotificationRequest或者我应该忽略那些弃用警告?
我正在制作我的第一个应用程序,其中一个功能是待办事项列表。我有一个开关,当它关闭时禁用本地通知,当它打开时,它应该在开关打开时启用它们。我使用 保存开关状态UserDefaults,通知确实出现,但是,即使我指定它们应该,它们也不会重复。现在我将通知设置为每 60 秒重复一次以进行测试,但是当它起作用时将需要两天时间。只要开关打开,如何使通知重复?我的开关代码:
@IBOutlet weak var switchout: UISwitch!
@IBAction func notifswitch(_ sender: Any) {
print ("hello")
if switchout.isOn
{
if #available(iOS 10.0, *), list.isEmpty == false {
let content = UNMutableNotificationContent()
content.title = "You have tasks to complete!"
content.subtitle = ""
content.body = "Open the task manager to see which tasks
need completion"
let alarmTime = Date().addingTimeInterval(60)
let components = Calendar.current.dateComponents([.weekday,
.hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching:
components, repeats: true)
let …Run Code Online (Sandbox Code Playgroud)