远程推送通知操作

Remote Push notifications actions

我创建了一个接收远程推送通知的代码,这个代码工作正常。现在我需要添加两个向左滑动并执行操作的 "buttons"。我知道下面的代码用于识别操作

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {  

    if identifier == "optin1" {  
        //do something  
    }  
    else identifier == "option2" {  
        //do something  
    }  

    completionHandler()  
}  

但我不知道如何创建向左滑动的按钮。我该怎么做

这是我的 AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool   
{   
    // Override point for customization after application launch.   
  let types:UIUserNotificationType = [.Alert, .Sound, .Badge]  


    application.registerForRemoteNotifications()   
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: nil))  

    initLocationManager()   

    return true   

}   
func application(application: UIApplication,    didFailToRegisterForRemoteNotificationsWithError error: NSError) {   
    //print(error)   
}   

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {   

    print(deviceToken)   

}   


func application(application: UIApplication, didReceiveRemoteNotificationuserInfo userInfo: [NSObject : AnyObject]) {   
    print(userInfo)   
}   

Update: new version Swift 4 compatible. This example was adapted from this amazing tutorial https://cocoacasts.com/actionable-notifications-with-the-user-notifications-framework

  1. 配置用户通知中心

    import UserNotifications
    ...
    UNUserNotificationCenter.current().delegate = self
    
  2. 定义动作

    let actionReadLater = UNNotificationAction(identifier: Constants.Action.readLater,
                                               title: "Read Later",
                                               options: [])
    
  3. 定义类别

    let tutorialCategory = UNNotificationCategory(identifier: Constants.Category.tutorial,
                                                  actions: [actionReadLater],
                                                  intentIdentifiers: [],
                                                  options: [])
    
  4. 注册类别

    UNUserNotificationCenter.current().setNotificationCategories([tutorialCategory])
    
  5. 从 IBAction 按实例安排本地通知

    // Request Notification Settings
    UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
        switch notificationSettings.authorizationStatus {
        case .notDetermined:
            self.requestAuthorization(completionHandler: { (success) in
                guard success else { return }
    
                // Schedule Local Notification
                self.scheduleLocalNotification()
            })
        case .authorized:
            // Schedule Local Notification
            self.scheduleLocalNotification()
        case .denied:
            print("Application Not Allowed to Display Notifications")
        case .provisional:
            print("provisional")
        }
    }
    

previous version

您可以将此代码复制并粘贴到 didFinishLaunchingWithOptions 方法中:

  1. 创建动作

    // NOTFICATION
    let incrementAction = UIMutableUserNotificationAction()
    incrementAction.identifier = "HI_ACTION"
    incrementAction.title = "Hi!"
    incrementAction.activationMode = UIUserNotificationActivationMode.Background
    incrementAction.authenticationRequired = false
    incrementAction.destructive = false
    
  2. 创建类别

    let counterCategory = UIMutableUserNotificationCategory()
    counterCategory.identifier = "HELLO_CATEGORY"
    
  3. 关联操作和类别

    counterCategory.setActions([incrementAction],
                               forContext: UIUserNotificationActionContext.Default)
    counterCategory.setActions([incrementAction],
                               forContext: UIUserNotificationActionContext.Minimal)
    
  4. 注册

    let categories = NSSet(object: counterCategory) as! Set<UIUserNotificationCategory>
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: categories)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    
  5. 最后一步仅做演示

    let notification = UILocalNotification()
    notification.alertBody = "Hey!"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.fireDate  = NSDate(timeIntervalSinceNow: 5)
    notification.category  = "HELLO_CATEGORY"
    
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
    

When the app is launched you have to push CMD+L

这是一个link to an example