オブジェクトの作成または更新に関するプッシュ通知を発生させる特定のクラスに対してCloudKitサブスクリプションを作成しました。iOS - プッシュ通知が期待通りに応答しない
このコードは以下の通り行く:
// Create the predicate
let predicate = NSPredicate(format: "recordId = %@", RECORD_ID)
// Create a subscription specifying the record type, predicate and notification options
let subscription = CKQuerySubscription(recordType: "Notes", predicate: predicate, options: [.firesOnRecordUpdate, .firesOnRecordCreation])
// Create a CloudKit notification object
let notificationInfo = CKNotificationInfo()
notificationInfo.shouldBadge = true
// Set the subscriptor's notification object to the new CloudKit notification object
subscription.notificationInfo = notificationInfo
// Save the subscription to the database
let publicDatabase = CKContainer.default().publicCloudDatabase
publicDatabase.save(subscription) { (subs, err) in
if let err = err {
print("Failed to save subscription:", err)
return
}
}
サブスクリプションがサーバーに正常に保存されます。
私は黙っ通知を作成したい、と(受信時)私は次のように通知を処理しています:
import UserNotifications
// Register for push notifications
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.badge, .alert, .sound]) { (success, err) in
if let err = err {
print("Failed to request oauth for notifications:", err)
}
}
application.registerForRemoteNotifications()
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("didRegisterForRemoteNotificationsWithDeviceToken:", deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register notifications_ error:", error)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("------------------------")
print("Receiving updates from the server")
// Convert the userInfo parameter to a CKNotification object
let cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo)
// Get the body of the notification
let alertBody = cloudKitNotification.alertBody
print("alertBody:", alertBody ?? "")
// Get the new or modified record form the CKQueryNotification object
if cloudKitNotification.notificationType == CKNotificationType.query {
guard let queryNotification = cloudKitNotification as? CKQueryNotification else {
print("could not cast the query notification")
return
}
let recordId = queryNotification.recordID
print("recordId:", recordId ?? "")
}
}
問題が起こるのはここです。
私がこのアプリを使用していて、アップデートがサーバーに送信された場合、didReceiveRemoteNotification
が起動し、すべて正常に動作しますが、アプリがバックグラウンドにある場合、プッシュ通知が発生するとapp_notificationバッジが1 didReceiveRemoteNotification
は受信されません。
私の質問は、到着時にプッシュ通知エントリを処理するにはどうすればいいですか?
私はApp downloads content from the network
とApp downloads content in response to push notifications
私は何のプッシュ通知を受信しないfalseに私のnotificationInfo.shouldBadge
を設定した場合、私は、気づいたもう一つとInfo.plis
ファイルにRequired background modes
を設定しました。
この問題を回避する方法についてのヒントはありますか? –