远程推送

//
//  AppDelegate.swift
//  远程推送
//
//  Created by admin on 16/5/5.
//  Copyright © 2016年 snsnb. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        // 8.0 之后需要授权
        if #available(iOS 8.0, *) {
            // 初始化设置对象
            let setting = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Badge,UIUserNotificationType.Sound,UIUserNotificationType.Alert], categories: nil)
            // 注册
            application.registerUserNotificationSettings(setting)
            // 注册远程通知
            application.registerForRemoteNotifications()
        } else {
            UIApplication.sharedApplication().registerForRemoteNotificationTypes([UIRemoteNotificationType.Badge,UIRemoteNotificationType.Sound,UIRemoteNotificationType.Alert])
        }
        // 和本地消息一样,在程序被杀死的状态下,也需要在这里获取下传递来的消息
        if launchOptions != nil {
            let lableRect = UIScreen.mainScreen().bounds
            let lable = UILabel(frame: lableRect)
            lable.numberOfLines = 0
            lable.backgroundColor = UIColor.grayColor()
            // 全部信息
            // lable.text = "\(launchOptions)"
            // 传递来的消息
            lable.text = "\(launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey])"
            self.window?.rootViewController?.view.addSubview(lable)
        }
        return true
    }
    // 获得设备标识,把标识纪录下,在给apple服务器发送的时候需要用到
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
    {
        print(deviceToken.description)
    }
    // 获得传递的消息
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
    {
        // 可以打印消息
        print(userInfo)
    }
}

标签: none

添加新评论