我在我的应用程序中安排了本地通知,现在当我向左侧滑动警报时,我得到一个通用的取消(交叉)按钮.
我很好奇我是否可以添加自定义按钮/操作,如下图所示?
我为你准备了一些剪切代码,在ViewDidLoad
方法显示后,用一个按钮显示通知10秒.
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应该非常简单.