【问题标题】:Actions Buttons Not Showing in Local Notification本地通知中未显示操作按钮
【发布时间】:2023-03-18 08:14:01
【问题描述】:

我正在使用 swift 中的本地通知。当我执行代码时,我没有在通知栏上看到 ACCEPT_IDENTIFIER 和 NOT_NOW_IDENTIFIER 之类的操作..我使用的是 iOS sdk 8.4

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
}

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

 var localNotification = UILocalNotification()
    localNotification.alertBody = "Testing Notification"
    localNotification.alertAction = "Open It"
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
    localNotification.category = "ActionDemoCategory"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    删除这个

    if notificationSettings.types == UIUserNotificationType.None

    【讨论】:

    • 谢谢..现在我被困在快速比较两个双精度值了..我们如何比较两个双精度值。
    • 试试if a-b &lt; 0.0001,意思是a==b表示Double
    猜你喜欢
    • 2017-05-04
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2013-08-17
    相关资源
    最近更新 更多