2016-04-20 23 views
1

在我的項目中,我遇到了一個問題:有兩個不同的通知。當有很多種通知時,我怎樣纔能有不同的UIUserNotificationSettings

其中一個需要兩個UIMutableUserNotificationActions(確定並取消),另一個只需要一個(稍後提醒我)。

下面是代碼:

let completeAction = UIMutableUserNotificationAction() 
completeAction.identifier = "OK" // the unique identifier for this action 
completeAction.title = "OK" // title for the action button 
completeAction.activationMode = .Background // UIUserNotificationActivationMode.Background - don't bring app to foreground 
completeAction.authenticationRequired = false // don't require unlocking before performing action 
completeAction.destructive = true // display action in red 

let cancelAction = UIMutableUserNotificationAction() 
cancelAction.identifier = "Cancel" 
if #available(iOS 9.0, *) { 
    cancelAction.parameters = [UIUserNotificationTextInputActionButtonTitleKey : "Send"] 
} else { 
    // Fallback on earlier versions 
} 
cancelAction.title = "Cancel" 
if #available(iOS 9.0, *) { 
    cancelAction.behavior = UIUserNotificationActionBehavior.TextInput 
} else { 
    // Fallback on earlier versions 
} 
cancelAction.activationMode = .Background 
cancelAction.destructive = false 
cancelAction.authenticationRequired = false 

let todoCategory = UIMutableUserNotificationCategory() // notification categories allow us to create groups of actions that we can associate with a notification 
todoCategory.identifier = "TODO_CATEGORY" 
todoCategory.setActions([cancelAction, completeAction], forContext: .Minimal) // UIUserNotificationActionContext.Default (4 actions max) 

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: NSSet(array: [todoCategory]) as? Set<UIUserNotificationCategory>)) 

但有隻有一個UIUserNotificationSettings條件。

+0

請勿將您的代碼作爲圖像發佈。用實際的代碼副本更新你的問題並粘貼到問題中(並正確格式化)。 – rmaddy

+0

@rmaddy我已修復。謝謝。這是我第一次問問題。 :d – Castiel

回答

0

最後,我找到了答案,並完美地解決了我的問題。我在這裏複製代碼,以防有人發現我遇到的同樣的問題。

有一種方式,IOS讓你有不同的通知操作,UIMutableUserNotificationCategory

let cancelCategory = UIMutableUserNotificationCategory() // notification categories allow us to create groups of actions that we can associate with a notification 
    cancelCategory.identifier = "Notification_Category" 
    cancelCategory.setActions([cancelAction], forContext: .Default) // 

而且

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: NSSet(array: [todoCategory,cancelCategory]) as? Set<UIUserNotificationCategory>) 

方法讓你有幾個通知類別。

最後一步。當你後端想要推送通知時。你應該建立一個鍵值的一部分(「category」=>「你的類別標識符」),並且每件事都做完了。

(body['aps'] = array('alert' => '','sound' => 'default','link_url' => '','category' => 'Notification_Category',);) 
相關問題