我花了最后5个小时试图找出如何在swift2.1中显示REMOTE推送通知中的操作。
事实: 1-我收到通知但没有显示选项 2-我能够生成一个没有任何问题的本地通知
代码:
enum Actions:String{
case increment = "INCREMENT_ACTION"
case decrement = "DECREMENT_ACTION"
case reset = "RESET_ACTION"
}
var categoryID:String {
get{
return "COUNTER_CATEGORY"
}
}
func registerNotification() {
//removeNotification()
// 1. Create the actions **************************************************
// increment Action
let incrementAction = UIMutableUserNotificationAction()
incrementAction.identifier = Actions.increment.rawValue
incrementAction.title = "Yes"
incrementAction.activationMode = UIUserNotificationActivationMode.Background
incrementAction.authenticationRequired = false
incrementAction.destructive = false
// decrement Action
let decrementAction = UIMutableUserNotificationAction()
decrementAction.identifier = Actions.decrement.rawValue
decrementAction.title = "No"
decrementAction.activationMode = UIUserNotificationActivationMode.Background
decrementAction.authenticationRequired = true
decrementAction.destructive = false
// reset Action
let resetAction = UIMutableUserNotificationAction()
resetAction.identifier = Actions.reset.rawValue
resetAction.title = "RESET"
resetAction.activationMode = UIUserNotificationActivationMode.Foreground
// NOT USED resetAction.authenticationRequired = true
resetAction.destructive = true
// 2. Create the category ***********************************************
// Category
let counterCategory = UIMutableUserNotificationCategory()
counterCategory.identifier = categoryID
// A. Set actions for the default context
counterCategory.setActions([incrementAction, decrementAction, resetAction],
forContext: UIUserNotificationActionContext.Default)
// B. Set actions for the minimal context
counterCategory.setActions([incrementAction, decrementAction],
forContext: UIUserNotificationActionContext.Minimal)
// 3. Notification Registration *****************************************
let types = UIUserNotificationType.Alert //| UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: counterCategory) as! Set<UIUserNotificationCategory>)
print("About to register remote notification")
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!) {
print("Got token data! \(deviceToken)")
print(deviceToken.hexString)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("Recived: \(userInfo)")
//Parsing userinfo:
var temp : NSDictionary = userInfo
if let info = userInfo["aps"] as? Dictionary<String, AnyObject>
{
var alertMsg = info["alert"] as! String
// var alert:UIAlertView! // alert = UIAlertView(标题:&#34;&#34;,消息:alertMsg,委托:nil,cancelButtonTitle:&#34; OK&#34;) // alert.show()
}
}
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
print("Im here")
if userInfo["category"] as! String == categoryID {
print("Yes")
let action:Actions = Actions(rawValue: identifier!)!
var counter = 0;
switch action{
case Actions.increment:
attemptReconnectionToSocket()
case Actions.decrement:
counter--
case Actions.reset:
counter=0
}
}
completionHandler()
}
帮助!!!