Cố gắng thiết lập thông báo đẩy cho iOS10. đã có nó làm việc trước đây cho các phiên bản dưới đây 10
đọc qua một số hướng dẫn cài đặt của tôi là như vậy:Gặp sự cố khi nhận thông báo đẩy trong iOS10 và iOS9
// Register for the defined notifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = UIApplication.shared.delegate as! AppDelegate
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if error == nil {
UIApplication.shared.registerForRemoteNotifications()
}
}
} else {
// Fallback on earlier versions
let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: [autoCancelCategory])
UIApplication.shared.registerUserNotificationSettings(notificationSettings)
UIApplication.shared.registerForRemoteNotifications()
}
này được gọi trong lần đăng nhập^trong một trong các bộ điều khiển quan điểm của tôi.
Và bây giờ trong AppDelegate
, nó phù hợp với UNUserNotificationCenterDelegate
, tôi có những phương pháp:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Got a push!")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
print("Got a push!")
}
Và tôi cũng có:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let settings = UserDefaults.standard
settings.setValue(deviceToken, forKey: "deviceToken")
}
Thiết bị token được tải lên máy chủ như Data
, và điều này đã làm việc tốt cho các phiên bản trước.
Tôi đã kiểm tra mã thông báo trên máy chủ khớp với mã trên điện thoại.
Tôi đã bật thông báo đẩy cho tất cả các mục tiêu trong Capabilities
, tôi cũng đã đánh dấu Add the push Notifications entitlement to your entitlements file
.
Không nhận được bất kỳ thứ gì cả khi đang đẩy.
Bất kỳ ý tưởng nào tôi có thể làm sai ở đây? Bất kỳ con trỏ nào cũng sẽ được đánh giá cao!
Cảm ơn!
EDIT: Tôi cũng nhận thấy rằng các thông báo đẩy không hoạt động trong iOS9.
Có thể là sự cố hệ điều hành. Vui lòng thử bằng cách khởi động lại thiết bị. https://twitter.com/yogye7/status/781340933228945408 –