• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift4.2打印devicetoken

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap