0

我有一个应用程序,当应用程序处于后台或未运行时,它将针对特定位置更改发送本地通知。我使用区域监控来获取位置更改并在需要时创建通知请求。我的问题是通知在 iOS 10 中无法正常工作,而在 iOS 11 和 12 中可以正常工作。下面是创建通知请求的代码。

func getRequest() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        if granted {
            DispatchQueue.main.async {
                self.scheduleNotification()
            }
        }
    }
}

func scheduleNotification() {
    let timeTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 15.0, repeats: false)
    let center = UNUserNotificationCenter.current()

    let content = UNMutableNotificationContent.init()
    content.title = "Notification works!"
    content.sound = UNNotificationSound.default

    let request = UNNotificationRequest.init(identifier: "LocalNotification", content: content, trigger: timeTrigger)

    center.add(request) { (error) in
        print(error?.localizedDescription ?? "No Error")
    }
}

有什么我想念的东西必须包含在 iOS 10 中吗?为什么它不能单独在 iOS 10 中运行?

4

1 回答 1

0

多亏了另一个帖子中的这个答案,在堆栈溢出上花费了几个小时之后。我们还应该提到content.body. 这在 iOS 10 中不应为空。由于某种原因,通知在 iOS 11 和 12 中没有正文。

于 2019-04-08T11:29:25.093 回答