1

我正在尝试让 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 应用没有收到远程通知调用呢?我在实际设备上使用该应用程序,而不是模拟器。谢谢。

在此处输入图像描述

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

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)")
            }
        }))
    }
}
4

1 回答 1

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. 尝试CKQuerySubscription在 CloudKit 仪表板中删除您的,然后再次运行注册它的 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方法返回什么?它说它成功还是失败?

于 2018-09-05T21:17:40.130 回答