Как открыть приложение и показать push-уведомление, когда приложение убито в ios - PullRequest
0 голосов
/ 14 мая 2018

когда приложение 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)

    }

}

1 Ответ

0 голосов
/ 14 мая 2018

Попробуйте этот code.setup ваш 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)
}
...