代码之家  ›  专栏  ›  技术社区  ›  Bartłomiej Semańczyk

在iOS 12下,UNUserNotification不会发出本地通知

  •  0
  • Bartłomiej Semańczyk  · 技术社区  · 7 年前

    import UserNotifications
    
    if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.alert, .sound]) { _, _ in }
            let content = UNMutableNotificationContent()
            content.title = "Hello Staff"
            content.body = "Customer just ordered a milk with croissant"
            content.sound = UNNotificationSound.default()
            let date = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date(timeIntervalSinceNow: 10))
            let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
            let request = UNNotificationRequest(identifier: "abcde", content: content, trigger: trigger)
            center.add(request)
    }
    

    AppDelegate . 但我看不到任何当地的通知。为什么?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Yury Imashev    7 年前

    你的代码运行良好。我想问题是,当你收到通知时,你的应用程序是活动的。 iOS 仅当应用程序未处于活动状态时显示系统通知。如果在触发通知时应用处于活动状态,则系统会触发 UNUserNotificationCenterDelegate 方法,以便您可以自己处理通知。

    10 秒,你需要运行你的应用程序,然后关闭它并等待 10 秒。如果你给予你的应用这样的权限,就会出现通知。