动作按钮未在本地通知中显示ios 9.2 swift 2 [英] Actions buttons not showing up in local notifications ios 9.2 swift 2

查看:78
本文介绍了动作按钮未在本地通知中显示ios 9.2 swift 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环创建通知,相关代码为:

I am creating notifications in a loop, relevant code is:

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

    serNotificationType = UIUserNotificationType.Alert.union(UIUserNotificationType.Sound).union(UIUserNotificationType.Badge)

    let completeAction = UIMutableUserNotificationAction()
    completeAction.identifier = "COMPLETE" // the unique identifier for this action
    completeAction.title = "Clear" // 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 callInAction = UIMutableUserNotificationAction()
    callInAction.identifier = "CALLIN"
    callInAction.title = "Call now"
    callInAction.destructive = false
    callInAction.authenticationRequired = false
    callInAction.activationMode = UIUserNotificationActivationMode.Background

    callInAction.destructive = false

    let notificationCategory = UIMutableUserNotificationCategory() // notification categories allow us to create groups of actions that we can associate with a notification
    notificationCategory.identifier = "CALLINNOTIFICATION"
    notificationCategory.setActions([callInAction, completeAction], forContext: .Default) //UIUserNotificationActionContext.Default (4 actions max)
    notificationCategory.setActions([completeAction, callInAction], forContext: .Minimal) //UIUserNotificationActionContext.Minimal - for when space is limited (2 actions max)
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: notificationTypes, categories: NSSet(array:[notificationCategory]) as? Set<UIUserNotificationCategory>))

    return true
}

通知的调度是在for循环中完成的(也许这是调度有问题的原因):

Scheduling of the notifications is done in the for loop (and maybe this is the reason something is wrong with scheduling):

func scheduleLocalNotifications() {

    let arrayOfEvents: [[Meeting]] = CalendarController.sharedInstance.getAllMeetings()

    //remove CallIn notifications anyway
    self.removeScheduledNotifications()

        var limitCounter = 0  //limit is 64 local notifications

        print("scheduling start: \(CallIn.Settings.notifyNumberOfMinutesBeforeEvent)")
        for var x = 0; x < arrayOfEvents.count; x++ {

        for var y = 0; y < arrayOfEvents[x].count; y++ {
            let event = arrayOfEvents[x][y]

            if(event.startTime.timeIntervalSinceDate(NSDate()) > -2000 && limitCounter <= 64){
                if(notificationsAreAllowed()){
                    let notification = UILocalNotification()
                    let minutesBefore = CallIn.Settings.notifyNumberOfMinutesBeforeEvent
                    notification.fireDate = event.startTime.dateByAddingTimeInterval(-minutesBefore * 60) //time of launch of notification

                    if(minutesBefore <= 1){
                        notification.alertBody = "Your \(event.title) is about to start"
                    }else{
                        notification.alertBody = "You have \(event.title) in \(Int(minutesBefore)) minutes"
                    }
                    notification.alertAction = "OK"
                    notification.soundName = UILocalNotificationDefaultSoundName
                    notification.userInfo = ["title": event.title, "UUID": event.UUID, "CallIn": "CallInNotification"]
                    notification.category = "CALLINNOTIFICATION"
                    notification.applicationIconBadgeNumber = 1

                    UIApplication.sharedApplication().scheduleLocalNotification(notification)
                }

                limitCounter += 1
            }
        }
    }
}

立即呼叫"和关闭"按钮未显示. 我想要的是:

The buttons Call now, and Dismiss are not showing up. What I want is this:

我得到的是:

推荐答案

好,我发现了问题,这可能对将来的某人有用:

Ok, I found the problem and this may be useful for someone in the future:

我有

UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

在应用程序的两个不同位置两次调用了

(因为我已经在应用程序中进行了设置,因此需要从设置页面进行更改). 因此,当重构并调用它一次后,它就起作用了.

was called twice in two different places in the application (because I had setting in the application, and needed to alter it from settings page). So when refactored, and called it once, it worked.

这篇关于动作按钮未在本地通知中显示ios 9.2 swift 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆