Ich habe ein CloudKit-Abonnement für eine bestimmte Klasse erstellt, die eine Push-Benachrichtigung bei der Objekterstellung oder Aktualisierung auslöst.iOS - Push-Benachrichtigungen reagieren nicht wie erwartet
Dieser Code geht wie folgt vor:
// 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
}
}
Das Abonnement erfolgreich auf dem Server gespeichert wird.
Ich wollte eine stille Benachrichtigung erstellen, und ich bin Umgang mit der Meldung wie folgt (beim Empfang):
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 ?? "")
}
}
Hier ist, wo das Problem auftritt.
Wenn ich die App verwende und ein Update an den Server gesendet wurde, wird meine didReceiveRemoteNotification
ausgelöst und alles funktioniert ordnungsgemäß. Wenn sich meine App jedoch im Hintergrund befindet, ändert sich mein app_notification-Badge (erhöht um 1) bei einer Push-Benachrichtigung wird empfangen, aber die didReceiveRemoteNotification
feuert nicht.
Meine Frage ist, wie kann ich den Push-Notification-Eintrag behandeln, wenn es ankommt?
ich die Required background modes
in der Info.plis
Datei gesetzt haben mit App downloads content from the network
und App downloads content in response to push notifications
Eine weitere Sache, bemerkte ich, wenn ich auf meine notificationInfo.shouldBadge
gesetzt falsch ich keine Push-Benachrichtigungen erhalten.
Vielen Dank für
helfen
Gibt es Hinweise, wie Sie dieses Problem umgehen können? –