AppDelegate从不获取要求CKQuerySubscription的didReceiveRemoteNotification

时间:2018-09-05 07:11:42

标签: ios swift cloudkit appdelegate remote-notifications

我正在尝试让iOS应用监听CKQuerySubscription的更改。数据通过远程iOS应用程序传输。我已经有一个macOS应用程序,该应用程序确实会接收远程iOS应用程序发送的数据。我遇到麻烦的iOS应用已订阅。但是,它的AppDelegate从未在didReceiveRemoteNotification方法中收到呼叫。

import UIKit
import UserNotifications
import CloudKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        /* notifications */
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        UNUserNotificationCenter.current().getNotificationSettings { (settings) in
            switch settings.authorizationStatus {
            case .authorized:
                print("You already have permission")
                DispatchQueue.main.async() {
                    application.registerForRemoteNotifications()
                }
            case .denied:
                print("setting has been disabled")
            case .notDetermined:
                print("Let me ask")
                UNUserNotificationCenter.current().requestAuthorization(options: []) { (granted, error) in
                    if error == nil {
                        if granted {
                            print("you are granted permission")
                            DispatchQueue.main.async() {
                                application.registerForRemoteNotifications()
                            }
                        }
                    }
                }
            }
        }
        return true
    }
}

    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("Receiving data...") // never called...
    }
}

我具有一些功能,如下所示。我不知道该应用是否需要push notifications。现在,它已打开。

那我的iOS应用为什么不接到远程通知电话?我将应用程序与实际设备而非模拟器配合使用。谢谢。

enter image description here

编辑:创建记录更改的订阅

class HomeViewController: UIViewController {
    override func viewDidLoad() {
        registerSubscription()
    }

    func registerSubscription() {
        let cloudContainer = CKContainer(identifier: "iCloud.com.xxx.XXXXX")
        let privateDB = cloudContainer.privateCloudDatabase
        let predicate = NSPredicate(format: "TRUEPREDICATE")
        let subscription = CKQuerySubscription(recordType: "PrivateRecords", predicate: predicate, options: .firesOnRecordCreation)
        let notification = CKNotificationInfo()
        subscription.notificationInfo = notification
        privateDB.save(subscription, completionHandler: ({returnRecord, error in
            if let err = error {
                print("Subscription has failed: \(err.localizedDescription)")
            } else {
                print("Subscription set up successfully")
                print("Subscription ID: \(subscription.subscriptionID)")
            }
        }))
    }
}

1 个答案:

答案 0 :(得分:1)

还有更多可以检查的内容。

首先,请确保在应用程序委托中实现didReceiveRemoteNotification

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {  
  let dict = userInfo as! [String: NSObject]
  let notification = CKNotification(fromRemoteNotificationDictionary: dict)

  if let sub = notification.subscriptionID{
    print("iOS Notification Received: \(sub)")
  }
}

您还可以检查其他几件事:

  1. 尝试在CloudKit仪表板中删除CKQuerySubscription,然后再次运行您的iOS代码进行注册。订阅会显示在仪表板上吗?
  2. CloudKit日志是否显示已发送通知?它列出了所有推送到设备的通知。
  3. 如果您使用的是静默推送通知,请尝试在“后台模式”功能中(在远程通知的正上方)启用背景获取

如果您执行所有操作后仍然无法使用,可以共享CKQuerySubscription代码吗?

-更新-

尝试在CKNotificationInfo对象上设置一些其他属性。有一些带有通知的晦涩错误,通常可以通过设置以下几个属性来规避:

notification.shouldSendContentAvailable = true
notification.alertBody = "" //(Yes, a blank value. It affects the priority of the notification delivery)

您还可以尝试将谓词设置为:NSPredicate(value: true)

此外,您的privateDB.save方法返回什么?它说成功还是失败?