[英]Actions buttons not showing up in local notifications ios 9.2 swift 2
我正在循環創建通知,相關代碼為:
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循環中完成的(也許這是調度有問題的原因):
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
}
}
}
}
好的,我發現了問題,這可能對將來的某人有用:
我有
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
在應用程序的兩個不同位置被兩次調用(因為我已經在應用程序中進行了設置,因此需要從設置頁面進行更改)。 因此,當重構並調用它一次時,它就起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.