AppDelegate 永远不会获得为 CKQuerySubscription 调用的 didReceiveRemoteNotification [英] AppDelegate Never Gets Its didReceiveRemoteNotification Called For CKQuerySubscription

查看:28
本文介绍了AppDelegate 永远不会获得为 CKQuerySubscription 调用的 didReceiveRemoteNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 iOS 应用侦听 CKQuerySubscription 更改.数据由远程 iOS 应用程序传输.我已经有一个 macOS 应用程序,它可以接收远程 iOS 应用程序发送的数据.我遇到问题的 iOS 应用程序已经订阅了.然而,它的 AppDelegate 从未收到 didReceiveRemoteNotification 方法中的调用.

I'm trying to let the iOS app listen to CKQuerySubscription changes. Data is transmitted by a remote iOS app. I already have a macOS application, which does receive data sent by the remote iOS app. The iOS app I have trouble with already has a subscription. Yet, its AppDelegate never receives a call in the didReceiveRemoteNotification method.

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...
    }
}

我有一些功能,如下所示.我不知道该应用是否需要推送通知.目前,它已开启.

I have some capabilities on as shown below. I don't know if the app needs push notifications. For now, it's turned on.

那么为什么我的 iOS 应用没有收到远程通知调用?我在实际设备上使用该应用程序,而不是模拟器.谢谢.

So why doesn't my iOS app get the remote notification call? I'm using the app with an actual device, not a simulator. Thanks.

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

EDIT: Creating a subscription to a record change

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

推荐答案

您还可以查看更多内容.

There are a few more things you can check.

首先,确保您在应用委托中实现了 didReceiveRemoteNotification:

First, make sure you implement didReceiveRemoteNotification in your app delegate:

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

您还可以检查其他一些事项:

There are also a few other things you can check:

  1. 尝试在 CloudKit 仪表板中删除您的 CKQuerySubscription,然后再次运行您的 iOS 代码来注册它.订阅是否显示在信息中心?
  2. CloudKit 日志是否显示已发送通知?它列出了推送到设备的所有通知.
  3. 如果您使用的是静默推送通知,请尝试在后台模式功能中启用后台提取(位于远程通知的正上方).
  1. Try deleting your CKQuerySubscription in the CloudKit dashboard, then run your iOS code again that registers it. Does the subscription show up in the dashboard?
  2. Does the CloudKit log show that a notification was sent? It lists all notifications that were pushed to a device.
  3. If you are using silent push notifications, try enabling Background fetch in the Background Modes capability (right above Remote notifications).

如果你做了所有这些但它仍然不起作用,你能分享你的 CKQuerySubscription 代码吗?

If you do all that and it still doesn't work, can you share your CKQuerySubscription code?

-- 更新 --

尝试在您的 CKNotificationInfo 对象上设置一些附加属性.通知有一些隐晦的错误,通常可以通过设置如下几个属性来规避:

Try setting some additional attributes on your CKNotificationInfo object. There are some obscure bugs with notifications that can usually be circumvented by setting a couple properties like this:

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

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

You can also try setting your predicate to: NSPredicate(value: true)

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

Also, what does your privateDB.save method return? Does it say it succeeds or fails?

这篇关于AppDelegate 永远不会获得为 CKQuerySubscription 调用的 didReceiveRemoteNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆