• swift4.2 打印devicetoken


    import UIKit
    import UserNotifications
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
        
        var window: UIWindow?
        
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
            
            if #available(iOS 10, *){
                iOS8Later()
                return true
            }
            
            if #available(iOS 8, *){
                iOS10Later()
                return true
            }
            early()
    
            return true
        }
        
        
        ///请求完成后会调用把获取的deviceToken返回给我们
        func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            
            testStringToData()
            
            //deviceToken = 32 bytes
            print("deviceToken = (deviceToken)")
            
            //FIXME:打印推送64位token
            print(deviceToken.map { String(format: "%02.2hhx", arguments: [$0]) }.joined() , "推送 deviceToken" )
        }
        
    
        func testStringToData(){
            let testStr = "莎莎哈"
            let testData = testStr.data(using: String.Encoding.utf8)
            let newTestStr = String(data: testData ?? Data(), encoding: String.Encoding.utf8)
            print("======", testStr, testData,  newTestStr)
        }
    
        
    }
    
    extension AppDelegate{
        
        func early(){
            let type = UIRemoteNotificationType.alert.rawValue | UIRemoteNotificationType.badge.rawValue | UIRemoteNotificationType.sound.rawValue
            UIApplication.shared.registerForRemoteNotifications(matching: UIRemoteNotificationType(rawValue: type))
        }
        
        func iOS8Later(){
            let type = UIUserNotificationType.badge.rawValue | UIUserNotificationType.alert.rawValue | UIUserNotificationType.sound.rawValue
            //请求授权
            let set = UIUserNotificationSettings(types: UIUserNotificationType(rawValue: type), categories: nil)
            
            UIApplication.shared.registerUserNotificationSettings(set)
            
            //需要通过设备UUID 和APP bundle ID 发送请求,获取deviceToken
            UIApplication.shared.registerForRemoteNotifications()
        }
        
        func iOS10Later(){
                let center = UNUserNotificationCenter.current()
                center.delegate = self
                center.requestAuthorization(options: UNAuthorizationOptions.alert) { (isSucceseed: Bool, error:Error?) in
                    
                    if isSucceseed == true{
                        print( "成功")
                    }else{
                        print( "失败")
                        print("error = (error)")
                    }
                }
                UIApplication.shared.registerForRemoteNotifications()
        }
    }
    
    extension AppDelegate: UNUserNotificationCenterDelegate{
        
    }
    

      

    Swift3.1的DeviceToken打印的是32Bytes
    https://www.jianshu.com/p/fed585eef7c1

  • 相关阅读:
    前端之CSS盒模型介绍
    前端之CSS列表及背景类属性
    前端之CSS字体和文本类属性
    流程控制语句
    前端之CSS语法及选择器
    前端之CSS创建的样式
    前端之HTML语法及常用标签
    前端之HTML样式
    网页制作之前端简介
    jQuery(一)、核心
  • 原文地址:https://www.cnblogs.com/qingzZ/p/10304720.html
Copyright © 2020-2023  润新知