swift3.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

转载于:https://juejin.im/post/5a41aedff265da43062b02e3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值