2016-04-20 6 views
1

In meinem Projekt habe ich ein Problem: Es gibt zwei verschiedene Benachrichtigungen.Wie kann ich verschiedene UIUserNotificationSettings haben, wenn es viele Arten von Benachrichtigungen gibt

Einer von ihnen brauchen zwei UIMutableUserNotificationActions (OK und Abbrechen) und der andere braucht nur einen (Erinnere mich später). Hier

ist der Code:

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>)) 

Aber es gibt nur eine Bedingung von UIUserNotificationSettings.

+0

Veröffentlichen Sie Ihren Code nicht als Bild. Aktualisiere deine Frage mit der tatsächlichen Code-Kopie und füge sie in die Frage ein (und richtig formatiert). – rmaddy

+0

@rmaddy ich habe behoben. Danke. es ist mein erstes Mal, Frage zu stellen. : D – Castiel

Antwort

0

Endlich fand ich eine Antwort und löste mein Problem perfekt. Ich kopiere den Code hier, falls jemand das gleiche Problem wie ich finde.

gibt es eine Möglichkeit, dass IOS können Sie verschiedene Benachrichtigungsaktionen haben, 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) // 

Und

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

Methode ermöglicht es Ihnen, mehrere Benachrichtigungskategorien haben.

Letzter Schritt. wenn du Backend willst, drücke eine Benachrichtigung. Sie sollten nur einen Teil des Schlüsselwerts ("category" => "your category identifier") erstellen, und alles ist erledigt.

(body['aps'] = array('alert' => '','sound' => 'default','link_url' => '','category' => 'Notification_Category',);) 
Verwandte Themen