我有本地通知,我有两种要执行的操作,我希望在用户收到通知时显示这些操作。
相关代码:
的AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
......
//local notifications part
let completeAction = UIMutableUserNotificationAction()
completeAction.identifier = "COMPLETE" // the unique identifier for this action
completeAction.title = "Complete" // 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 in"
callInAction.activationMode = .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([callInAction, completeAction], forContext: .Minimal) //UIUserNotificationActionContext.Minimal - for when space is limited (2 actions max)
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: Set<UIUserNotificationCategory>.init(arrayLiteral: notificationCategory))) // we're now providing a set containing our category as an argument
return true
}
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
//var item = TodoItem(deadline: notification.fireDate!, title: notification.userInfo!["title"] as String, UUID: notification.userInfo!["UUID"] as String!)
switch (identifier!) {
case "COMPLETE":
print("complete")
//do nothing for now
case "CALLIN":
print("gonnacall")
let call = Call()
call.withDevicePhone(method: Call.AS.UNDEFINED)
default: // switch statements must be exhaustive - this condition should never be met
print("Error: unexpected notification action identifier: \(identifier)")
}
completionHandler() // per developer documentation, app will terminate if we fail to call this
}
安排通知方法:
func scheduleLocalNotifications() {
....
if(event.startTime.timeIntervalSinceDate(NSDate()) > 0 && limitCounter <= 64){
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
}
.....
}