CloudKit CKSubscription没有通知? [英] CloudKit CKSubscription without notifications?

查看:260
本文介绍了CloudKit CKSubscription没有通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CloudKit编写Swift应用程序。当设备在CloudKit中修改记录时,我希望在其他设备的本地存储中更新相应的记录,而不显示推送通知。

I'm writing a Swift app with CloudKit. When a record is modified in CloudKit by a device, I want the corresponding records to be updated in the local storage of the other devices without displaying a push notification.

我是否需要在 didFinishLaunchingWithOptions 中调用 registerUserNotificationSettings (意味着用户必须接受我的应用程序的通知)即使我不喜欢打算显示任何推送通知?

Do I need to call registerUserNotificationSettings in didFinishLaunchingWithOptions (meaning that the user has to accept the notifications for my app) even if I don't plan to display any push notification?

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))


推荐答案

是的,你需要拨打 registerUserNotificationSettings 甚至你需要的只是后台远程通知。因此用户将提示通知权限。这没有任何意义,因为用户不会看到通知,但就是这样。

Yes you do need to call registerUserNotificationSettings even all you need is background remote notification. So user will be prompt for notifications permission. It makes no sense as users will not be seeing the notifications but that's how it is.

我用它来设置它:

func application(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {
let settings = UIUserNotificationSettings(forTypes:.None,categories:nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}

确保在调用CloudKit saveSubscription时提供 shouldSendContentAvailable = true 。以下代码用于订阅自定义区域:

Make sure when you call CloudKit saveSubscription you provide shouldSendContentAvailable = true. The following code is for subscription for a custom zone:

let subscription = CKSubscription(zoneID:zoneID, options: CKSubscriptionOptions(rawValue: 0))

let notificationInfo = CKNotificationInfo()
notificationInfo.shouldSendContentAvailable = true

subscription.notificationInfo = notificationInfo

CKContainer.defaultContainer().privateCloudDatabase.saveSubscription(subscription) { subscription, error in
}

你也是需要在项目的Xcode下启用后台模式功能,并选中远程通知框。

You also need to enable Background Modes capability under Xcode for your project, and tick the box Remote Notifications.

用户可以转到设置应用程序以禁用应用程序的通知。但您仍会收到CloudKit服务器的远程通知触发器。

User can go to Settings app to disable notifications for your app. But you will still receive remote notification trigger by CloudKit server.

在AppDelegate中实现以下功能以接收远程通知:

Implement the following functions in your AppDelegate to receive remote notifications:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {}

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {}

这篇关于CloudKit CKSubscription没有通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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