当 iOS 应用程序已经在后台运行时,我可以点击 msg 并进入应用程序并查看通知视图
但是应用程序被杀了,我无法进入应用程序并查看自定义通知视图 这是我用来加载自定义通知视图控制器的重写方法
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let json = JSON(userInfo)
var title = ""
var content = ""
NSLog("Received a Notificat`ion: (json)")
if let messageID = json["aps"]["alert"]["title"].string{
print("Message ID: (messageID)")
title = messageID
}
if let body = json["aps"]["alert"]["body"].string{
print("Message ID: (body)")
content = body
}
print(json["message"].string)
let payloadObj = convertStringToDictionary(text: json["message"].string ?? "")
print(payloadObj)
var CustomerName = ""
var CustomerContactNo = ""
if let type = payloadObj?["Type"]{
let _notificationType = type as? String
if(_notificationType == "ORDERSTATUS"){
name = (payloadObj?["Name"]as? String)!
CustomerName = (payloadObj?["CustomerName"]as? String)!
CustomerContactNo = (payloadObj?["CustomerContactNo"]as? String)!
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "NotificationViewController") as! NotificationViewController
viewController.Name = CustomerName
viewController.contact = CustomerContactNo
if let navigationController = self.window?.rootViewController as? UINavigationController
{
navigationController.pushViewController(viewController, animated: true)
}
}
completionHandler(UIBackgroundFetchResult.newData)
}
}
试试这段代码。像这样设置AppDelegate文件代码...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.registerForPushNotifications()
return true
}
func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: (granted)")
guard granted else { return }
self.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: (settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
let aps = userInfo["aps"] as! [String: AnyObject]
let dic = aps as NSDictionary
//here set your push any viewcontroller logic
completionHandler()
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let aps = userInfo["aps"] as! [String: AnyObject]
let dic = aps as NSDictionary
//here set your push any viewcontroller logic
completionHandler(.newData)
}