【问题标题】:Local Notification content going into a loop in iOS本地通知内容进入 iOS 循环
【发布时间】:2020-10-11 10:29:39
【问题描述】:

我正在尝试在我的应用中实现本地通知。这个想法是每天早上 9:00 向用户发送一个通知,其中包含不同的报价,但我遇到了一个错误,即始终显示通知的相同内容,即无休止地重复相同的报价。我该如何解决?这是我正在使用的代码,我尝试为每个发送的通知使用 UUID,但它没有带来改进。

    let notificationCenter = UNUserNotificationCenter.current()
    let options: UNAuthorizationOptions = [.alert, .sound]
    notificationCenter.requestAuthorization(options: options) {
        (didAllow, error) in
        if !didAllow {
            print("User has declined notifications")
        }
    }

    notificationCenter.getNotificationSettings { (settings) in
      if settings.authorizationStatus != .authorized {
        print("Notifications not allowed")
      }
    }
    
    let randomArrayNotificationQuote = Int(arc4random_uniform(UInt32(myQuotes.count)))
    let randomArrayNotificationTitle = Int(arc4random_uniform(UInt32(myTitle.count)))
    
    let content = UNMutableNotificationContent()
    content.title = "\(myTitle[randomArrayNotificationTitle])"
    content.body = "\(myQuotes[randomArrayNotificationQuote])"
    content.sound = UNNotificationSound.default
    content.categoryIdentifier = "com.giovannifilippini.philo"
    
    // Deliver the notification
    
    var dateComponents = DateComponents()
    dateComponents.hour = 9
    dateComponents.minute = 00
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    
    let uuid = UUID().uuidString
    
    let request = UNNotificationRequest.init(identifier: uuid, content: content, trigger: trigger)
    
    notificationCenter.add(request) { (error) in
        if error != nil {
            print("add NotificationRequest succeeded!")
            notificationCenter.removePendingNotificationRequests(withIdentifiers: [uuid])
        }
    }

【问题讨论】:

  • 为什么repeats 设置为true?
  • 我不是用它来重复通知吗?
  • 如果您想发送不同的通知,请不要这样做。
  • 我尝试将值从 true 更改为 false,但现在我收到一般通知时遇到问题

标签: ios swift notifications


【解决方案1】:

这里的问题是触发器将重复设置为 true

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

您需要将其设置为 false 并在每次要安排不同的通知时重新运行此方法(不同的内容)

为了在应用程序处于前台时接收通知,您需要从 UNUserNotificationCenterDelegate 实现 willPresent 方法

这是一个简单的例子:

class ViewController: UIViewController {
    
    private let userNotificaionCenter = UNUserNotificationCenter.current()

    override func viewDidLoad() {
        super.viewDidLoad()
        // 1. Assign the delegate
        userNotificaionCenter.delegate = self
        scheduleNotification()
    }

    func scheduleNotification() {
      // Same notification logic that you have
    }
 }

// 2. Implement the UNUserNotificationCenterDelegate
extension ViewController: UNUserNotificationCenterDelegate {
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // This will allow the app to receive alerts and sound while in the foreground
        completionHandler([.alert, .sound])
    }
}

【讨论】:

  • 我已将值从 true 更改为 false 并设置触发器以每 60 秒发送一次通知 (let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: false)) 以查看结果,但我无法在以下位置收到任何通知全部
  • 如果您在应用程序后台运行,您会在 60 秒后看到通知吗?如果是这样,您将需要实现 UNUserNotificationDelegate 并且更具体地说是 willPresent 方法。详情请查看答案编辑。
  • 并且加上更新答案中的代码,应用关闭时的逻辑是一样的,即使应用不在后台,用户也能收到通知
  • 应用程序在后台时总是会收到通知,willPresent 委托方法允许您配置应用程序在前台时如何处理通知。如果您不实现此方法,系统将使用此方法的默认实现调用completionHandler([.none]),这将导致系统不显示通知。更多信息可以查看官方Apple documentation
  • 我明白了,当应用程序在后台运行时效果很好,但我也在尝试在应用程序未在后台打开时发送通知
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
相关资源
最近更新 更多