操作按钮未显示在本地通知中

时间:2015-08-05 08:55:20

标签: ios swift

我正在使用swift中的本地通知。当我执行代码时,我没有获得像ACCEPT_IDENTIFIER和NOT_NOW_IDENTIFIER ...在通知栏中的操作。我正在使用iOS sdk 8.4

$state

这段代码我写入viewDidLoad()方法

func application(application: UIApplication, didFinishLaunchingWithOptions   launchOptions: [NSObject: AnyObject]?) -> Bool {

    let notificationSettings : UIUserNotificationSettings = UIApplication.sharedApplication().currentUserNotificationSettings()

    if notificationSettings.types == UIUserNotificationType.None
    {
        var notificationTypes : UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge

        let notificationAction1:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
        notificationAction1.identifier = "ACCEPT_IDENTIFIER"
        notificationAction1.title = "Ok"
        notificationAction1.activationMode = UIUserNotificationActivationMode.Foreground
        notificationAction1.destructive = true
        notificationAction1.authenticationRequired = false

        let notificationAction2:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
        notificationAction2.identifier = "NOT_NOW_IDENTIFIER"
        notificationAction2.title = "No"
        notificationAction2.activationMode = UIUserNotificationActivationMode.Background
        notificationAction2.destructive = false
        notificationAction2.authenticationRequired = false


        let actionArrayDefault = NSArray(objects: notificationAction1,notificationAction2)
        let actionArrayMinimal = NSArray(objects: notificationAction1)

        var actionDemoCategory = UIMutableUserNotificationCategory()
        actionDemoCategory.identifier = "ActionDemoCategory"
        actionDemoCategory.setActions(actionArrayDefault as [AnyObject], forContext: UIUserNotificationActionContext.Default)
        actionDemoCategory.setActions(actionArrayMinimal as [AnyObject], forContext: UIUserNotificationActionContext.Minimal)

        let categoryForSettings = NSSet(objects: actionDemoCategory)

        let newNotificationSetting = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoryForSettings as Set<NSObject>)

        UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSetting)

    }

    return true
}

1 个答案:

答案 0 :(得分:-1)

删除此

if notificationSettings.types == UIUserNotificationType.None