gpt4 book ai didi

ios - 如何打开应用程序并在 ios 中终止应用程序时显示推送通知

转载 作者:行者123 更新时间:2023-11-28 23:58:41 28 4
gpt4 key购买 nike

当 Ios 应用程序已经在后台运行时,我可以点击消息并进入应用程序并可以查看通知 View

但是应用程序已被终止,我无法进入应用程序并查看自定义通知 View 这是我用来加载自定义通知 View Controller 的覆盖方法

    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)
}

关于ios - 如何打开应用程序并在 ios 中终止应用程序时显示推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50324762/

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