2015-09-07 51 views
2

背景在獲得初始許可後,是否可以將新操作添加到UIUserNotificationCategory?

問題

在iOS版9的發佈,我想「升級」我的用戶和支持推送通知這個新的「在線回覆」選項...但很明顯,我首先需要添加UIMutableUserNotificationAction(用新的行爲屬性)到每個用戶的UIApplication.sharedApplication().currentUserNotificationSettings()

問題

因爲我已經有一般權限的通知(徽章/聲音/警報),我能,悄悄重新執行UIApplication.sharedApplication().registerUserNotificationSettings並添加新的「在線回覆」行動?

回答

0

事實證明,你不能簡單地打電話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>))) 

     } 
     } 
    } 
    } 
} 
+1

代碼難以理解。如果您有意見,請將它們格式化爲評論。 – matt

相關問題