gpt4 book ai didi

ios - 如何向 UILocalNotification 警报添加操作按钮/操作?

转载 作者:可可西里 更新时间:2023-11-01 04:15:25 29 4
gpt4 key购买 nike

我在我的应用程序中安排了一个本地通知,现在当我向左滑动警报时,我会看到一个通用的取消(十字)按钮。

我很好奇我是否可以像下图那样向它添加自定义按钮/操作?

enter image description here

最佳答案

我为您准备了一些代码片段,它在 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 应该非常简单。

关于ios - 如何向 UILocalNotification 警报添加操作按钮/操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34144510/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com