背景
- 我有一个iOS消息应用程序,已有一个已安装的用户群
- 我已经向用户请求/获得了系统级通知权限(也就是说,我用类别/操作调用了
UIApplication.sharedApplication().registerUserNotificationSettings
( - iOS 9在
UIMutableUserNotificationAction
上提供了一个名为behavior
的新属性,该属性允许通过.TextInput
选项进行在线回复
问题
iOS 9发布后,我想"升级"我的用户,并在推送通知上支持这个新的"在线回复"选项。。。但显然,我首先需要向每个用户的UIApplication.sharedApplication().currentUserNotificationSettings()
添加一个新的UIMutableUserNotificationAction
(具有新的行为属性(
问题
由于我已经拥有一般通知权限(徽章/声音/警报(,我可以安静地重新执行UIApplication.sharedApplication().registerUserNotificationSettings
并添加新的"在线回复"操作吗?
事实证明,您不能简单地调用UIApplication.sharedApplication().registerUserNotificationSettings
并添加单个新操作——这样做将只注册单个新操作(并删除所有其他操作(。
以下是我在application: didFinishLaunchingWithOptions
:中完成的操作
注意:我使用count < 2
的逻辑检查,因为在我的应用程序的以前版本中,我只在目标类别("REPLY_category"(中建立了一个操作。
// This code ensures that iOS 9 Users (especially iOS 8 ==> iOS 9 upgraders) will be able to see
// iOS 9's new text input 'behavior', available for notifications quick-actions
if #available(iOS 9, *) {
if NSUserDefaults.standardUserDefaults().boolForKey("asked_for_notification_permission") {
let settings: UIUserNotificationSettings = UIApplication.sharedApplication().currentUserNotificationSettings()!
for category in settings.categories! {
let cat: UIUserNotificationCategory = category
if cat.identifier == "REPLY_CATEGORY" {
if cat.actionsForContext(UIUserNotificationActionContext.Default)!.count < 2 {
print("! aD: iOS 9 User has less than the expected reply actions")
// Re-build entire categories / actions here, including
// the new action with .behavior = .TextInput
// THEN, simply re-register:
let notificationCategories = NSSet(array: allCategories)
let settings: UIUserNotificationType = [.Sound, .Alert, .Badge]
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: settings, categories: (notificationCategories as! Set<UIUserNotificationCategory>)))
}
}
}
}
}