如何在不点击横幅或显示通知之前访问推送通知响应?

时间:2018-06-15 04:51:29

标签: ios swift push-notification apple-push-notifications

我以这种方式在我的应用中实现了推送通知

//MARK:- Register notifications

func registerForPushNotifications() {

    if #available(iOS 10.0, *){

        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            if (granted)
            {
                UIApplication.shared.registerForRemoteNotifications()
            }
            else{
                //Do stuff if unsuccessful...
            }
            // Enable or disable features based on authorization.
        }
    }

    else
    {
        //If user is not on iOS 10 use the old methods we've been using
        let types: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
        let settings: UIUserNotificationSettings = UIUserNotificationSettings( types: types, categories: nil )
        UIApplication.shared.registerUserNotificationSettings( settings )
        UIApplication.shared.registerForRemoteNotifications()

    }

}

//MARK: Push Notifications Delegate Methods

func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) {

    var token = ""

    for i in 0..<deviceToken.count {
        //token += String(format: "%02.2hhx", arguments: [chars[i]])
        token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }

    USER_DEFAULTS.setValue(token, forKey: "Device_ID")

    USER_DEFAULTS.synchronize()

}

func application( _ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error ) {
    print( error.localizedDescription )

}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {

    UIApplication.shared.applicationIconBadgeNumber = 0

    alertRemoteNotification(userInfo as NSDictionary)
}

//Code for showing alert when in foreground

func alertRemoteNotification(_ userInfo : NSDictionary)
{
    if UIApplication.shared.applicationState == .active {

        if let aps = userInfo as? NSDictionary {

            if let apsDidt = aps.value(forKey: "aps") as? NSDictionary {

                if let alertDict = apsDidt.value(forKey: "alert") as? NSDictionary {

                    if let notification_type = alertDict.value(forKey: "name") as? String {

                        if let notification_Message = alertDict.value(forKey: "body") as? String {

                            let alert = UIAlertController(title: notification_type.capitalized + " Alert", message: notification_Message, preferredStyle: UIAlertControllerStyle.alert)
                            let okayBtn = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in

                                // When Okay

                            UIApplication.shared.applicationIconBadgeNumber = 0

                                if #available(iOS 10.0, *) {

                                    let center = UNUserNotificationCenter.current()
                                    center.removeAllDeliveredNotifications() // To remove all delivered notifications
                                    center.removeAllPendingNotificationRequests()
                                } else {
                                    // Fallback on earlier versions
                                    UIApplication.shared.cancelAllLocalNotifications()
                                }

                                let rootViewController = self.window!.rootViewController as! UINavigationController
                                let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                                let dashBoardVC = mainStoryboard.instantiateViewController(withIdentifier: "DashBoardVC") as! DashBoardVC
                                rootViewController.pushViewController(dashBoardVC, animated: false)                                    
                            })
                            let cancelBtn = UIAlertAction(title: "Cancel", style: .default, handler: { (action) -> Void in

                                UIApplication.shared.applicationIconBadgeNumber = 0

                                if #available(iOS 10.0, *) {

                                    let center = UNUserNotificationCenter.current()
                                    center.removeAllDeliveredNotifications() // To remove all delivered notifications
                                    center.removeAllPendingNotificationRequests()
                                } else {
                                    // Fallback on earlier versions
                                    UIApplication.shared.cancelAllLocalNotifications()
                                }
                            })
                            alert.addAction(okayBtn)
                            alert.addAction(cancelBtn)

                            self.window?.rootViewController!.present(alert, animated: true, completion: nil)
                        }
                    }
                }
            }
        }
    }
    else {

        let rootViewController = self.window!.rootViewController as! UINavigationController
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let dashBoardVC = mainStoryboard.instantiateViewController(withIdentifier: "DashBoardVC") as! DashBoardVC
        rootViewController.pushViewController(dashBoardVC, animated: false)
    }
}

//Delegate methods

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    completionHandler([.sound, .alert, .badge])

    UIApplication.shared.applicationIconBadgeNumber = 0

}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    let userInfo = response.notification.request.content.userInfo as NSDictionary

    completionHandler()

    self.alertRemoteNotification(userInfo as NSDictionary)
}

我可以在点击通知横幅后访问响应,但实际问题是当我在前台时,我需要显示带有通知响应的警报而不点击通知横幅。请告诉我如何在不点击通知横幅的情况下获得回复。

3 个答案:

答案 0 :(得分:4)

iOS 10+提供委托userNotificationCenter:willPresentNotification:withCompletionHandler

  

询问代表如何处理通知时到达的通知   应用程序正在前台运行。

只有在应用程序打开时才会调用。

此外,您可以使用CONTENT-AVAILABLE=1来触发方法。

FLOW :(没有编带通知,content-available:1

应用程序已打开状态: - willPresentNotification(ios10 +) - &gt; didReceiveRemoteNotification:fetchCompletionHandler

背景中的应用: - didReceiveRemoteNotification:fetchCompletionHandler

应用已关闭: - 您不会收到通知数据,除非通过点击通知打开该应用

替代方法:使用丰富通知

您可以使用通知扩展程序来创建自定义推送通知(包括图像/视频的内容)。 通知服务扩展&amp;用于实现此目的的通知内容扩展mutable-content:1需要触发此操作。在这里,您可以下载图像,获取数据等。[但是数据只能通过UserDefaults(应用程序组)与App共享,如果我错了,纠正我]

您可以search获取一些随机教程

答案 1 :(得分:2)

@available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        completionHandler([.sound, .alert, .badge])

        UIApplication.shared.applicationIconBadgeNumber = 0

        // Added This line
        alertRemoteNotification(notification.request.content.userInfo as NSDictionary) 
    }

无任何问题地工作。

答案 2 :(得分:1)

创建通知服务扩展以处理通知数据。您将获得30秒通过通知服务扩展

处理推送通知

Refer this screenshot

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    if let copy = request.content.mutableCopy() as? UNMutableNotificationContent {
        // Process your notification here

        contentHandler(copy)
    }
}

希望这会对你有所帮助

相关问题