【发布时间】:2023-03-18 08:14:01
【问题描述】:
我正在使用 swift 中的本地通知。当我执行代码时,我没有在通知栏上看到 ACCEPT_IDENTIFIER 和 NOT_NOW_IDENTIFIER 之类的操作..我使用的是 iOS sdk 8.4
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let notificationSettings : UIUserNotificationSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
if notificationSettings.types == UIUserNotificationType.None
{
var notificationTypes : UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Sound | UIUserNotificationType.Badge
let notificationAction1:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationAction1.identifier = "ACCEPT_IDENTIFIER"
notificationAction1.title = "Ok"
notificationAction1.activationMode = UIUserNotificationActivationMode.Foreground
notificationAction1.destructive = true
notificationAction1.authenticationRequired = false
let notificationAction2:UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationAction2.identifier = "NOT_NOW_IDENTIFIER"
notificationAction2.title = "No"
notificationAction2.activationMode = UIUserNotificationActivationMode.Background
notificationAction2.destructive = false
notificationAction2.authenticationRequired = false
let actionArrayDefault = NSArray(objects: notificationAction1,notificationAction2)
let actionArrayMinimal = NSArray(objects: notificationAction1)
var actionDemoCategory = UIMutableUserNotificationCategory()
actionDemoCategory.identifier = "ActionDemoCategory"
actionDemoCategory.setActions(actionArrayDefault as [AnyObject], forContext: UIUserNotificationActionContext.Default)
actionDemoCategory.setActions(actionArrayMinimal as [AnyObject], forContext: UIUserNotificationActionContext.Minimal)
let categoryForSettings = NSSet(objects: actionDemoCategory)
let newNotificationSetting = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoryForSettings as Set<NSObject>)
UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSetting)
}
return true
}
我将这段代码写入 viewDidLoad() 方法
var localNotification = UILocalNotification()
localNotification.alertBody = "Testing Notification"
localNotification.alertAction = "Open It"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
localNotification.category = "ActionDemoCategory"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
【问题讨论】: