【问题标题】:Stop a notification using swift 3使用 swift 3 停止通知
【发布时间】:2017-06-30 07:42:36
【问题描述】:

我遇到了一个涉及停止本地通知的问题。这个停止按钮是这样的,所以如果用户启动一个计时器(一旦计时器用完就会弹出一个通知),然后决定他们不再需要这个计时器,他们可以取消它。这里的问题是我尝试过的每种方法都不会阻止通知关闭。这主要是因为这些方法已经过时了。下面是我的代码,我想知道是否有人对如何阻止通知关闭有任何想法。任何帮助将不胜感激。

//In the view did load section
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
        if granted {
            print("granted")
        } else {
            print("not granted")
        }
    }


//in a seperate function
let center = UNUserNotificationCenter.current()

content.title = "Local notification"
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.init(named: "1.mp3")
content.badge = 1

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)
let request = UNNotificationRequest(identifier: "requestAlarm", content: content, trigger: trigger)
center.add(request)

center.delegate = self

【问题讨论】:

  • 我没有看到任何取消代码。
  • 是的,我已将其删除,因为正如我所提到的,它在 IOS 10 中普遍贬值

标签: swift xcode timer localnotification cancellation


【解决方案1】:

您可以通过以下方式停止某些通知:

center.removePendingNotificationRequests(withIdentifiers: ["alarm"])

其中“警报”是您的类别标识符。这样你就不会停止每一个通知,只是选择一个。好东西。 问候。

【讨论】:

    【解决方案2】:

    告诉用户通知中心removeAllPendingNotificationRequests

    【讨论】:

    • 我是按原样添加还是在函数中添加?
    • 好的,我有答案了,非常感谢您的帮助:) 我使用 center.removeAllPendingNotificationRequests()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    相关资源
    最近更新 更多