iOSの通知の設定、アプリケーションのフロントとバックのコールバックメソッド

関連するプッシュビットを注文するために時間を割いて、iOS版はオンラインパイル原理を押して、ここではそれらを繰り返さないでいると、登録、登録成功コールバック、コールバックメソッドの印象とプッシュの前と後に台湾をクリックするなど、整理しました。(スウィフト版)

まず、プッシュ登録

アプリケーションのAppDelegate方法(_アプリケーション:.のUIApplication、 didFinishLaunchingWithOptionsはで登録
サンプルコード:

        //注册推送通知
        if #available(iOS 10.0, *) {
            let notifCenter = UNUserNotificationCenter.current()
            notifCenter.delegate = self
            notifCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (registered, error) in
                #if DEBUG
                if registered {
                    print("notification authorization granted")
                } else {
                    print("notification authorization not granted,\nerror: \(String(describing: error))")
                }
                #endif
            }
            // 实现代理的方法, 接收到通知会调用.
        } else {
            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
        }
        UIApplication.shared.registerForRemoteNotifications()

        // 获取远程推送消息 iOS 10 已取消
        let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any]
        // 如果remote不为空,就代表应用在未打开的时候收到了推送消息
        if notification != nil {
            // 收到推送消息实现的方法
            UIApplication.shared.applicationIconBadgeNumber = 0
            self.gotoViewControllerWithType(userInfo: notification!)
        }

第二に、プッシュ登録の成功、失敗のコールバック

登録が成功トークンが(オンデマンド)サーバ障害のアラートに報告された後に表示されます。
サンプルコード:

//推送注册成功
    func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
#if DEBUG
        print("Device Token: \(token)")
#endif
        //上报服务器token
        self.reportToken(token: token)
    }
    //推送注册失败
    func application(_ application: UIApplication,didFailToRegisterForRemoteNotificationsWithError error: Error) {
//        print("registerError:\(error.localizedDescription)")
        //展示alert        
        let alertVc = UIAlertController(title: nil, message: error.localizedDescription, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertVc.addAction(action)
        self.window?.rootViewController?.present(alertVc, animated: true, completion: nil)
    }

第三に、我々は、表示回数やクリック数の通知を受け

APPが開始されていないときまず、(殺される)、私は、APPが、その後、図オーロラプッシュコールバックを参照して、特定の通知コールバックの下で、できないコールバックで通知します。


9893751-6638782869593894.png
通知コールバック.PNG

サンプルコード:

//接受通知
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        DDLogDebug("userInfo10:\(userInfo)")
        
        if UIApplication.shared.applicationState == .active {
        //应用在前台         
        }else{
            //推送后的动作
            self.gotoViewControllerWithType(userInfo: userInfo)
        }
    }
    //10.0之前展示、点击代理,iOS10.0之后展示代理
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler handler: @escaping (UIBackgroundFetchResult) -> Void) {
//        mxPrint("Notification received: \(userInfo)")
        
        let extra = userInfo[AnyHashable("extra")] as! [AnyHashable: Any]
        let type = extra[AnyHashable("type")] as! String
   
        if UIApplication.shared.applicationState == .active {

        }else{
            self.gotoViewControllerWithType(userInfo: userInfo)
        }
    }

まずこれは、私を修正してください問題があります。

ます。https://www.jianshu.com/p/b4f3e1d6e8cfで再現

おすすめ

転載: blog.csdn.net/weixin_33795806/article/details/91190473