我长期以来一直在努力解决这个问题,很久很久以前就已经开始工作了,但现在还没有。这是App Delegate中交互式通知的代码。如下所示。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
self.notificationSettings(application)
return true
}
func notificationSettings(application: UIApplication) {
let notificationActionOk :UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionOk.identifier = "ACCEPT_IDENTIFIER"
notificationActionOk.title = "Ok"
notificationActionOk.destructive = false
notificationActionOk.authenticationRequired = false
notificationActionOk.activationMode = UIUserNotificationActivationMode.Background
let notificationActionCancel :UIMutableUserNotificationAction = UIMutableUserNotificationAction()
notificationActionCancel.identifier = "NOT_NOW_IDENTIFIER"
notificationActionCancel.title = "Not Now"
notificationActionCancel.destructive = true
notificationActionCancel.authenticationRequired = false
notificationActionCancel.activationMode = UIUserNotificationActivationMode.Background
let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "ss"
notificationCategory .setActions([notificationActionOk,notificationActionCancel], forContext: UIUserNotificationActionContext.Default)
notificationCategory .setActions([notificationActionOk,notificationActionCancel], forContext: UIUserNotificationActionContext.Minimal)
application.registerUserNotificationSettings(
UIUserNotificationSettings(
forTypes: [.Alert, .Badge, .Sound],
categories: Set<UIUserNotificationCategory>(arrayLiteral: notificationCategory) ))
}
我在App Delegate中处理了这样的recievedNotification方法,如下所示。
// For Notifications, Local And Remote
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
application.applicationIconBadgeNumber = 0
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
{
print("didReceiveLocalNotification")
application.applicationIconBadgeNumber = 0
if application.applicationState == UIApplicationState.Active
{
print("Active")
}
else if application.applicationState == UIApplicationState.Background
{
print("Background")
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
else if application.applicationState == UIApplicationState.Inactive
{
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
print("Inactive")
}
}
// Local Notification, Listener
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void)
{
let _ = responseInfo[UIUserNotificationActionResponseTypedTextKey]
}
在我的视图控制器的某个地方,我有这个代码用于激活本地通知。下面给出的本地通知代码。
override func viewDidLoad() {
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.alertBody = "hello good morning"
localNotification.category = "ss"
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
我不知道为什么这不起作用,通知没有显示。我运行应用程序,来到视图控制器,以便本地通知将获得计划。我按下&#34;移动h&#34;,以便模拟器进入主屏幕。我在这里等了很长时间才收到通知,但什么都没发生。我做错了什么?或iOS 9.2需要交互式通知的任何特殊配置吗?