【问题标题】:Repeating local notification removes previous pending local notifications重复本地通知会删除先前待处理的本地通知
【发布时间】:2018-08-07 07:41:05
【问题描述】:

我想每 30 分钟发送一次本地通知。我已经实现了重复本地通知,但它删除了前面的本地通知。场景解释如下: 我的客户想要获得夜间警报。他希望早上醒来时可以立即查看所有通知提醒。

代码如下:

func application(_ application: UIApplication,  didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: {didAllow,error in  })
    return true
}
func applicationDidEnterBackground(_ application: UIApplication) {
    let content = UNMutableNotificationContent()
    content.title = "Hello"
    content.subtitle = "I am your local notification"
    content.body = "Yippppiiiieee...."
    content.badge = 1
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
    let request = UNNotificationRequest(identifier: "testing", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

}

【问题讨论】:

    标签: ios swift localnotification unnotificationrequest unnotificationtrigger


    【解决方案1】:

    首先,您不应使用与删除已安排的标识符相同的标识符

     let request = UNNotificationRequest(identifier: "testing", content: content, trigger: trigger)
    

    第二个你必须插入不同的 TimeInterval

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
    

    //

    (1...10).forEach {
    
        let content = UNMutableNotificationContent()
        content.title = "Hello \($0)"
        content.subtitle = "I am your local notification \($0)"
        content.body = "Yippppiiiieee.... \($0)"
        content.badge = 1
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval:TimeInterval($0*1800), repeats: false)
        let request = UNNotificationRequest(identifier: "testing\($0)", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    
    }
    

    【讨论】:

    • 当我想在每 30 分钟后收到通知时,如何设置不同的时间间隔
    • 唯一标识符可以生成随机字符串,30分钟的时间间隔,可以在当前时间加上30分钟。
    • 使用它每 30 分钟重复一次 让 trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1800, repeats: true)
    • 以上行将每 30 分钟重复一次相同的通知,如果您需要不同的正文/内容,则将其插入到 for 循环中并设置不同的标识符,重复设置为 false
    • 当您的第一个通知触发时,didreceivelocalnotification 函数将执行,您可以在其中编写代码以使用 UNTimeIntervalNotificationTrigger(timeInterval: 1800, repeats: true) 触发通知。
    【解决方案2】:

    先前的待处理通知被取消,因为您创建了一个具有相同identifier 的新通知。根据documentation

    如果您提供唯一标识符,系统会创建一个新通知。

    如果标识符与之前发送的通知匹配,系统会再次提醒用户,用新通知替换旧通知,并将新通知放在列表顶部。

    如果标识符与待处理请求匹配,新请求将替换待处理请求。

    解决方案是始终使用新标识符创建UNNotificationRequest

    var notificationCount = 0
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        // (...)
        notificationCount += 1
        let request = UNNotificationRequest(identifier: "testing\(notificationCount)", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }
    

    【讨论】:

    • 再次收到 3 次通知后,它开始删除之前的通知。
    【解决方案3】:

    不要使用相同的标识符。它将覆盖之前的通知。

    现在你会问,如果我每次都生成一个随机字符串,我将如何识别哪个通知被点击了?

    假设您有一个名为“coffeeTimeNotificationID”的标识符。只需将时间戳值 ([NSDate timeIntervalSinceReferenceDate]]) 附加到此字符串即可。现在您的字符串将如下所示:“coffeeTimeNotificationID1232134314”。

    每当用户点击此通知时,只需在标识符字符串中搜索“coffeeTimeNotificationID”。如果你找到它,你就会知道点击了哪种类型的通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多