是否可以在获得初始权限后向 UIUserNotificationCategory 添加新操作?
Is it possible to add a new action to UIUserNotificationCategory after obtaining initial permission?
背景
- 我有一个 iOS 消息传递应用程序,具有现有的已安装用户群。
- 我已经要求用户/获得了系统级通知权限(也就是,我已经调用
UIApplication.sharedApplication().registerUserNotificationSettings
类别/操作)。
- iOS 9 offers a new property on
UIMutableUserNotificationAction
called behavior
which allows for in-line replies via the .TextInput
option.
问题
在 iOS 9 发布后,我想 "upgrade" 我的用户并支持这个新的 "in-line reply" 推送通知选项......但显然我首先需要 向每个用户的UIApplication.sharedApplication().currentUserNotificationSettings()
添加一个新的UIMutableUserNotificationAction
(具有新行为属性)
问题
由于我已经拥有一般通知权限(徽章/声音/警报),我可以安静地重新执行UIApplication.sharedApplication().registerUserNotificationSettings
并添加新的"in-line reply"行动?
事实证明,您不能简单地调用 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>)))
}
}
}
}
}
背景
- 我有一个 iOS 消息传递应用程序,具有现有的已安装用户群。
- 我已经要求用户/获得了系统级通知权限(也就是,我已经调用
UIApplication.sharedApplication().registerUserNotificationSettings
类别/操作)。 - iOS 9 offers a new property on
UIMutableUserNotificationAction
calledbehavior
which allows for in-line replies via the.TextInput
option.
问题
在 iOS 9 发布后,我想 "upgrade" 我的用户并支持这个新的 "in-line reply" 推送通知选项......但显然我首先需要 向每个用户的UIApplication.sharedApplication().currentUserNotificationSettings()
UIMutableUserNotificationAction
(具有新行为属性)
问题
由于我已经拥有一般通知权限(徽章/声音/警报),我可以安静地重新执行UIApplication.sharedApplication().registerUserNotificationSettings
并添加新的"in-line reply"行动?
事实证明,您不能简单地调用 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>)))
}
}
}
}
}