iOS推送(2)
2015-10-15 11:33
405 查看
前一篇转载的文章说明了要如何生成cer 文件。在这个文章中,主要想记录下,<span style="font-family: Arial, Helvetica, sans-serif;">AppDelegate 中要怎么做。如下:</span>
//1.推送的形式:标记,声音,提示 <strong> func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {</strong> // Override point for customization after application launch. initAPNs() return true } //2. APNS <strong> func initAPNs() {</strong> print("AppDelegate.....initAPNs......") let app = UIApplication.sharedApplication() if DeviceUtils.isIOS7() { let types: UIRemoteNotificationType = [UIRemoteNotificationType.Badge , UIRemoteNotificationType.Sound , UIRemoteNotificationType.Alert] app.registerForRemoteNotificationTypes(types) } else { if #available(iOS 8.0, *) { let types: UIUserNotificationType = [UIUserNotificationType.Badge , UIUserNotificationType.Sound , UIUserNotificationType.Alert] let setting = UIUserNotificationSettings(forTypes: types, categories: nil) app.registerUserNotificationSettings(setting) app.registerForRemoteNotifications() } else { // Fallback on earlier versions } } } //3. 注册成功保存 deviceToken <strong>func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {</strong> let apnDeviceTokens = deviceToken.toHexString() as String let session = NSUserDefaults.standardUserDefaults() session.setValue(apnDeviceTokens, forKey: "apn_device_token") } //4.处理推送消息 <strong> func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {</strong> dealWithRemoteNotification(userInfo) } <strong>func dealWithRemoteNotification(userInfo: [NSObject : AnyObject]?) {</strong> if UIApplication.sharedApplication().applicationState == UIApplicationState.Active { return } let type = userInfo?["type"] as? String if type == "follow" { let user = userInfo?["user"] as? NSDictionary if let uid = user?["id"] as? String { gotoUserVCWith(uid) } } else if type == "live" { let cast = userInfo?["cast"] as? NSDictionary if let token = cast?["token"] as? String { UIApplication.trackEvent(TrackEvent.CONTENT_CONSUMPTION, action: TrackEvent.VIDEO_SELECTED, label: "REMOTE_NOTIFICATION") gotoVideoPlayerWith(token: token) } } else if type == "pick" { let cast = userInfo?["cast"] as? NSDictionary if let token = cast?["token"] as? String { UIApplication.trackEvent(TrackEvent.CONTENT_CONSUMPTION, action: TrackEvent.VIDEO_SELECTED, label: "REMOTE_NOTIFICATION") gotoVideoPlayerWith(token: token) } } else if type == "like" { let cast = userInfo?["cast"] as? NSDictionary if let token = cast?["token"] as? String { UIApplication.trackEvent(TrackEvent.CONTENT_CONSUMPTION, action: TrackEvent.VIDEO_SELECTED, label: "REMOTE_NOTIFICATION") gotoVideoPlayerWith(token: token) } } else if type == "link" { if let target = userInfo?["target"] as? String { let url: NSURL = NSURL(string: target)! UIApplication.sharedApplication().openURL(url) } } }
相关文章推荐
- iOS推送(1)
- iOS 多线程:NSThread和runloop
- iOS--工具--instruments
- 多线程
- iOS 心得四 GCD倒计时的写法
- iOS 闭包中的[weak self]在什么情况下需要使用,什么情况下可以不加?
- iOS容易造成循环引用的三种场景
- Nagios介绍
- ios 基于NSConnection简单封装的工具类
- iOS Font
- 剥开ios 系统sandbox神秘面纱
- iOS:iOS中的几种动画
- ios-html-get/post差额,简而言之(MS)CheckST
- iOS应用审核的通关秘籍
- iOS代码实践总结
- iOS每日一记之——————点击按钮进入到国家选择列表并回调
- 如何从iOS Developer 中下载xcode dmg
- IOS 瀑布流
- 多态
- iOS通过经纬度计算两个坐标之间的距离