[当我创建CKSubscription时,在iOS上可以正常调用didReceiveRemoteNotification,但在MacOS上则不能。我遇到了一个2015 SO线程,该线程正在讨论一个错误,建议的解决方法是将通知信息的soundName设置为空字符串-不幸的是,这并不能为我解决问题。
这是我注册远程通知的方式:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let subscription = CKQuerySubscription(recordType: "Reminder", predicate: NSPredicate(format: "TRUEPREDICATE"), options: [.firesOnRecordCreation, .firesOnRecordUpdate])
// Here we customize the notification message
let info = CKSubscription.NotificationInfo()
info.shouldSendContentAvailable = true
info.desiredKeys = ["identifier", "title", "date"]
info.soundName = ""
subscription.notificationInfo = info
// Save the subscription to Private Database in Cloudkit
CKContainer.default().privateCloudDatabase.save(subscription, completionHandler: { subscription, error in
if error == nil {
// Subscription saved successfully
} else {
// Error occurred
}
})
}
使用类方法UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
}
}
要使其正常工作,您需要在方法didFinishLaunchingWithOptions中进行调用>
UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .badge]) {
(granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}else{
print(error ?? "Error registered notifications")
}
}
}
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate