UNUserNotification无法在真实设备上直观显示通知

时间:2017-12-06 11:55:42

标签: ios iphone swift notifications

我的通知在模拟器上工作正常,但在我的设备上(运行iOS 10.3.3)通知根本没有显示(手机在触发时锁定)但手机在触发时会振动并且应用程序确实更新为正确设置所以我知道通知被触发 - 它只是没有在设备上直观显示。我错过了什么吗?

这是我在AppDelegate中设置通知授权的地方:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    locationManager.delegate = self

    // Setup notifications authorization request
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        // Check if granted, if not then notify user
        if granted {
            print("Notification access granted")
        } else {
            print(error?.localizedDescription ?? "General Error: notification access not granted")
            self.window?.rootViewController?.showAlertApplicationSettings(forErorType: .turnOnNotifications)
        }
    }

AppDelegate中的位置触发器:

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    if region is CLCircularRegion {
        print("DidEnter: \(region.identifier)")
        handleEvent(forRegion: region)
    }
}

处理从核心数据获取数据然后发送到AppDelegate中的notifyUser方法的事件代码:

func handleEvent(forRegion region: CLRegion) {
    guard let reminder = getReminder(fromRegionIdentifier: region.identifier) else {
        // There was a problem access the notification data, inform user
        notifyUser(title: "Reminder notifiction error", subtitle: "One of your notifications has just been triggered but error restriving notification data", notes: nil)
        return
    }

    notifyUser(title: reminder.titleString, subtitle: "Reminder has been triggered", notes: reminder.notesString)
    updateRemindersState(reminder: reminder)
}

这是AppDelegate中的实际通知触发器代码:

func notifyUser(title: String, subtitle: String, notes: String?) {
    // show an alert if applocation is active
    if UIApplication.shared.applicationState == .active {
        window?.rootViewController?.showAlert(title: title, message: subtitle)
    } else {
        let notification = UNMutableNotificationContent()
        notification.title = title
        notification.subtitle = subtitle
        if let notes = notes {
            notification.body = notes
        }
        notification.sound = UNNotificationSound.default()
        let request = UNNotificationRequest(identifier: "Notification", content: notification, trigger: nil)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
            if let error = error {
                print(error)
            }
        })
    }
}

P.S。检查我的设备在设置中已显示在通知中心,声音,锁定屏幕上显示,所有横幅都已打开。

0 个答案:

没有答案