使用 UNUserNotificationCenter 支持 iOS 10 个丰富的通知,但通知永远不会出现。它只会发出警报
Using UNUserNotificationCenter to support iOS 10 rich notifications, but the notification never shows up. It only sounds the alert
不确定,缺少什么,但我遇到了一个奇怪的问题!!!
通知会响起,但不会显示。
我已将应用程序注册为仅显示警报和声音,但奇怪的是它从未显示过。可以看到通知添加成功。但是,似乎有些琐碎的事情我无法理解。
即使删除通知也有效。
import Foundation
import NotificationCenter
import UserNotifications
class NotificationManager: NSObject {
private let categoryIdentifier = "notificationCategory"
private var toDateComponents = NSDateComponents()
private enum actionIdentifier : String {
case openApp = "openApp"
case playMusic = "playMusic"
}
// MARK: - Register for notifications
func registerForNotifications(application: UIApplication) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.currentNotificationCenter().getNotificationSettingsWithCompletionHandler { notificationSettings in
switch notificationSettings.authorizationStatus {
case .NotDetermined:
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Alert, .Sound]) { (granted, error) in
// Enable or disable features based on authorization
if granted {
print("Access requested and was granted for sending notifications")
UNUserNotificationCenter.currentNotificationCenter().setNotificationCategories([])
UNUserNotificationCenter.currentNotificationCenter().delegate = self
self.setupNotificationActions()
} else {
print("Access requested and was denied for sending notifications")
print(error?.localizedDescription)
}
}
case .Denied:
print("Notifications are turned off!!")
break // nothing to do, pointless to go on
case .Authorized:
self.setupNotificationActions()
}
}
} else {
// Nothing to do here
print("iOS 9 says, Let's get started...")
self.setupNotificationActions()
}
}
// MARK: - Setup notification actions
private func setupNotificationActions() {
// Initialize and specify the notification actions
if #available(iOS 10.0, *) {
let openAppAction = UNNotificationAction(identifier: actionIdentifier.openApp.rawValue, title: "Open App", options: [.AuthenticationRequired, .Foreground])
let playMusicAction = UNNotificationAction(identifier: actionIdentifier.playMusic.rawValue, title: "Play Music", options: [])
let notificationCategory = UNNotificationCategory(identifier: categoryIdentifier, actions: [openAppAction, playMusicAction], intentIdentifiers: [], options: [.CustomDismissAction])
UNUserNotificationCenter.currentNotificationCenter().setNotificationCategories([notificationCategory])
} else {
// Specify the notification actions
let openAppAction = UIMutableUserNotificationAction()
openAppAction.identifier = actionIdentifier.openApp.rawValue
openAppAction.title = "Open App"
openAppAction.activationMode = UIUserNotificationActivationMode.Foreground
openAppAction.destructive = false
let playMusicAction = UIMutableUserNotificationAction()
playMusicAction.identifier = actionIdentifier.playMusic.rawValue
playMusicAction.title = "Play Music"
playMusicAction.activationMode = UIUserNotificationActivationMode.Background
playMusicAction.destructive = false
playMusicAction.authenticationRequired = false
// Specify the category related to the above actions
let notificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = categoryIdentifier
notificationCategory.setActions([openAppAction, playMusicAction], forContext: UIUserNotificationActionContext.Default)
notificationCategory.setActions([playMusicAction], forContext: UIUserNotificationActionContext.Minimal)
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: NSSet(object: notificationCategory) as? Set<UIUserNotificationCategory>)
// if (notificationSettings.types != UIUserNotificationType.None){
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
// }
}
}
并且,在 AppDelegate 中...
private let notificationManager = NotificationManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
notificationManager.registerForNotifications(application)
}
应正确分配代表。虽然,我无法将发生这种情况的原因归零,但以下更改可确保通知通过。
AppDelegate,
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
private let notificationManager = NotificationManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UNUserNotificationCenter.currentNotificationCenter().delegate = self
notificationManager.registerForNotifications()
}
}
在 AppDelegate 本身中插入委托方法。
无论何时发送丰富的通知,请确保有效载荷,必须包含 "mutable-content": 1,
有效负载,
{
"aps": {
"alert": "Test Rich Notification",
"mutable-content": 1,
"badge": "1"
},
"mediaUrl": "https://www.awaragroup.com/wp-content/uploads/2015/05/it-specialist.jpg",
"mediaType": "jpg"
}
不确定,缺少什么,但我遇到了一个奇怪的问题!!!
通知会响起,但不会显示。
我已将应用程序注册为仅显示警报和声音,但奇怪的是它从未显示过。可以看到通知添加成功。但是,似乎有些琐碎的事情我无法理解。
即使删除通知也有效。
import Foundation
import NotificationCenter
import UserNotifications
class NotificationManager: NSObject {
private let categoryIdentifier = "notificationCategory"
private var toDateComponents = NSDateComponents()
private enum actionIdentifier : String {
case openApp = "openApp"
case playMusic = "playMusic"
}
// MARK: - Register for notifications
func registerForNotifications(application: UIApplication) {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.currentNotificationCenter().getNotificationSettingsWithCompletionHandler { notificationSettings in
switch notificationSettings.authorizationStatus {
case .NotDetermined:
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Alert, .Sound]) { (granted, error) in
// Enable or disable features based on authorization
if granted {
print("Access requested and was granted for sending notifications")
UNUserNotificationCenter.currentNotificationCenter().setNotificationCategories([])
UNUserNotificationCenter.currentNotificationCenter().delegate = self
self.setupNotificationActions()
} else {
print("Access requested and was denied for sending notifications")
print(error?.localizedDescription)
}
}
case .Denied:
print("Notifications are turned off!!")
break // nothing to do, pointless to go on
case .Authorized:
self.setupNotificationActions()
}
}
} else {
// Nothing to do here
print("iOS 9 says, Let's get started...")
self.setupNotificationActions()
}
}
// MARK: - Setup notification actions
private func setupNotificationActions() {
// Initialize and specify the notification actions
if #available(iOS 10.0, *) {
let openAppAction = UNNotificationAction(identifier: actionIdentifier.openApp.rawValue, title: "Open App", options: [.AuthenticationRequired, .Foreground])
let playMusicAction = UNNotificationAction(identifier: actionIdentifier.playMusic.rawValue, title: "Play Music", options: [])
let notificationCategory = UNNotificationCategory(identifier: categoryIdentifier, actions: [openAppAction, playMusicAction], intentIdentifiers: [], options: [.CustomDismissAction])
UNUserNotificationCenter.currentNotificationCenter().setNotificationCategories([notificationCategory])
} else {
// Specify the notification actions
let openAppAction = UIMutableUserNotificationAction()
openAppAction.identifier = actionIdentifier.openApp.rawValue
openAppAction.title = "Open App"
openAppAction.activationMode = UIUserNotificationActivationMode.Foreground
openAppAction.destructive = false
let playMusicAction = UIMutableUserNotificationAction()
playMusicAction.identifier = actionIdentifier.playMusic.rawValue
playMusicAction.title = "Play Music"
playMusicAction.activationMode = UIUserNotificationActivationMode.Background
playMusicAction.destructive = false
playMusicAction.authenticationRequired = false
// Specify the category related to the above actions
let notificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = categoryIdentifier
notificationCategory.setActions([openAppAction, playMusicAction], forContext: UIUserNotificationActionContext.Default)
notificationCategory.setActions([playMusicAction], forContext: UIUserNotificationActionContext.Minimal)
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: NSSet(object: notificationCategory) as? Set<UIUserNotificationCategory>)
// if (notificationSettings.types != UIUserNotificationType.None){
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
// }
}
}
并且,在 AppDelegate 中...
private let notificationManager = NotificationManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
notificationManager.registerForNotifications(application)
}
应正确分配代表。虽然,我无法将发生这种情况的原因归零,但以下更改可确保通知通过。
AppDelegate,
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
private let notificationManager = NotificationManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UNUserNotificationCenter.currentNotificationCenter().delegate = self
notificationManager.registerForNotifications()
}
}
在 AppDelegate 本身中插入委托方法。
无论何时发送丰富的通知,请确保有效载荷,必须包含 "mutable-content": 1,
有效负载,
{
"aps": {
"alert": "Test Rich Notification",
"mutable-content": 1,
"badge": "1"
},
"mediaUrl": "https://www.awaragroup.com/wp-content/uploads/2015/05/it-specialist.jpg",
"mediaType": "jpg"
}