如何在ios中杀死应用程序时打开应用程序并显示推送通知

时间:2018-05-14 07:07:26

标签: ios firebase apple-push-notifications firebase-cloud-messaging

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

但应用程序被杀了我无法进入应用程序并查看自定义通知视图 这是我用来加载自定义通知视图控制器

的覆盖方法
    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)

    }

}

1 个答案:

答案 0 :(得分:1)

试试这个代码。设置你的 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)
}