首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AppDelegate从未为CKQuerySubscription调用过它的didReceiveRemoteNotification

AppDelegate从未为CKQuerySubscription调用过它的didReceiveRemoteNotification
EN

Stack Overflow用户
提问于 2018-09-05 07:11:42
回答 1查看 499关注 0票数 1

我试图让iOS应用程序听到CKQuerySubscription的变化。数据由远程iOS应用程序传输。我已经有了一个macOS应用程序,它确实接收远程iOS应用程序发送的数据。我遇到麻烦的iOS应用程序已经订阅了。然而,它的AppDelegate从未在didReceiveRemoteNotification方法中接收到调用。

代码语言:javascript
复制
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应用程序没有接到远程通知电话呢?我用的是一个实际的设备,而不是模拟器。谢谢。

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

代码语言:javascript
复制
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)")
            }
        }))
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-09-05 21:17:40

还有几件事你可以检查。

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

代码语言:javascript
复制
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对象上设置一些附加属性。有些带有通知的模糊bug通常可以通过设置以下几个属性来规避:

代码语言:javascript
复制
notification.shouldSendContentAvailable = true
notification.alertBody = "" //(Yes, a blank value. It affects the priority of the notification delivery)

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52179137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档