美文网首页
swift3.0 推送页面跳转

swift3.0 推送页面跳转

作者: 嗯哼丶傻大个是你 | 来源:发表于2017-04-27 12:04 被阅读0次

    在AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.main.bounds)
        let tabBarVC = TBTabBarController()
        window!.rootViewController = tabBarVC
        window!.makeKeyAndVisible()
        
        let set = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(set)
        
        
        // 程序被杀死时跳转页面
        if let options = launchOptions {
          if let notification = options[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification  {
            let userInfo = notification.userInfo
            let apsInfo = userInfo?["id"] as? String
            // 展示推送的信息
            let alert = UIAlertView(title: "\(userInfo!)", message: nil, delegate: nil, cancelButtonTitle: "确定")
            alert.show()
            if apsInfo == "detail" {
              //页面跳转
              let VC = UIStoryboard(name: "Discover", bundle: nil).instantiateViewController(withIdentifier: "Message") as! MessageDetailViewController
              VC.isForNotification = true
              let nc = TBNavigationController(rootViewController: VC)
              self.window?.rootViewController?.present(nc, animated: true, completion: nil)
            }
          }
        }
        return true
      }
    
    

    本地通知页面跳转

    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
        //程序后台
        if UIApplication.shared.applicationState == UIApplicationState.inactive {
          guard let userInfos = notification.userInfo else {
            return
          }
          let apsInfo = userInfos["id"] as? String
          print(userInfos)
          if apsInfo == "detail" {
            let VC = UIStoryboard(name: "Discover", bundle: nil).instantiateViewController(withIdentifier: "Message") as! MessageDetailViewController
            let nc = TBNavigationController(rootViewController: VC)
            VC.isForNotification = true
            self.window?.rootViewController?.present(nc, animated: true, completion: nil)
          }
        }
        //程序前台
        if application.applicationState == UIApplicationState.active {
          guard let userInfo = notification.userInfo else {
            return
          }
    //      let alert = UIAlertView(title: "\(userInfo)", message: nil, delegate: nil, cancelButtonTitle: "确定")
    //      alert.show()
        }
      }
    

    远程推送

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        let tabBarVC = TBTabBarController()
        window!.rootViewController = tabBarVC
    // 程序进入后台
        if UIApplication.shared.applicationState == UIApplicationState.inactive {
          if TBUser.currentUser.logined {
              let apsInfo = userInfo["aps"] as! NSDictionary
              print(userInfo)
          }
        }
      }
    

    在viewController

    let noti = UILocalNotification()
        noti.repeatInterval = NSCalendar.Unit.minute
        noti.fireDate = Date().addingTimeInterval(-1*60)   // 每分钟推送一次
        noti.timeZone = NSTimeZone.default
        noti.alertBody = "推送消息"
        noti.alertTitle = "test"
        noti.soundName = UILocalNotificationDefaultSoundName
        noti.userInfo = ["id": "detail"]
        noti.applicationIconBadgeNumber = 1
        noti.alertAction = "跳转"
        UIApplication.shared.scheduleLocalNotification(noti)
    

    移除所有通知

      UIApplication.shared.cancelAllLocalNotifications()
    

    有兴趣的话 可以下载Demo: https://github.com/BJGX/LocalNotification

    相关文章

      网友评论

          本文标题:swift3.0 推送页面跳转

          本文链接:https://www.haomeiwen.com/subject/knpgzttx.html