zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ubuntu11.04更改默认JDK
    10个实用的jQuery交互/通信插件和教程
    jquery 使用方法
    在没有安装 ASP.NET MVC3 的服务器上运行 MVC3
    固定 vs. 流动 vs. 弹性:哪种布局更适合你?[SM]
    提升设计品质的8种布局方案[SM]
    Ubuntu 手动安装JDK
    十个简单好用的设计技巧[SM]
    jQuery VSDoc下载地址
    Ubuntu 配置Apache+PHP+MySQL
  • 原文地址:https://www.cnblogs.com/qingzZ/p/10304720.html
Copyright © 2011-2022 走看看