4
我在我的应用程序中安排了本地通知,现在当我向左滑动警报时,我会得到一个通用取消(交叉)按钮。如何将操作按钮/操作添加到UILocalNotification警报中?
我很好奇,如果我可以添加自定义按钮/操作像它在下面的图像?
我在我的应用程序中安排了本地通知,现在当我向左滑动警报时,我会得到一个通用取消(交叉)按钮。如何将操作按钮/操作添加到UILocalNotification警报中?
我很好奇,如果我可以添加自定义按钮/操作像它在下面的图像?
我对您准备了一些剪断代码示出了具有一个按钮10第二通知没有示出ViewDidLoad
方法之后。
import UIKit
class TestViewController: UIViewController {
let category = UIMutableUserNotificationCategory()
override func viewDidLoad() {
super.viewDidLoad()
let restartAction = UIMutableUserNotificationAction()
restartAction.identifier = "xx"
restartAction.destructive = false
restartAction.title = "Restart"
restartAction.activationMode = .Background
restartAction.authenticationRequired = false
let categoryIdentifier = "category.identifier"
category.identifier = categoryIdentifier
category.setActions([restartAction], forContext: .Minimal)
category.setActions([restartAction], forContext: .Default)
let categories = Set(arrayLiteral: category)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: categories)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotif = UILocalNotification()
localNotif.alertBody = "testBody"
localNotif.category = categoryIdentifier
// Notification will be shown after 10 second (IMPORTANT: if you want to see notification you have to close or put app into background)
localNotif.fireDate = NSDate().dateByAddingTimeInterval(10)
UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
}
}
注意:你必须处理在AppDelegate的方法操作:
func application(application: UIApplication, handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification, completionHandler:() -> Void) {
completionHandler()
}
当然我的代码是不是干净,因为它应该是,但你要知道,我写的只为呈现目的。
此代码是用Swift编写的,但是转换为Objective C应该非常简单。