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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 上房不到六年,401庄正家的房子竟装修了三次。这年头又不能置地,谁不想住得舒适一点呢,自然就把钱花到的房子上。装修...
    湖边人老刘阅读 425评论 0 1
  • 翱翔人生阅读 173评论 0 0
  • 这本书的中文名叫做《商业的本质》,作者是鼎鼎大名的韦尔奇夫妇。但是我更喜欢它的英文名《Real-life MBA》...
    颜小婧阅读 850评论 0 3
  • 与其静候外部改变,不如从自身行动起来,分析现状,选择最适合自己的路径。 这边呆的太辛苦,已经做了好多,可是仍旧未果...
    Elsa蜕变之路阅读 98评论 0 0
  • 唐叔说的话听进去了一些。中途的时候触及到一些敏感的话题,忍不住眼眶打湿了。 他说女孩子总归是要嫁人的,嫁的那个人不...
    无叶麦拉苏阅读 207评论 0 0