ios 在 Swift 4 中获取远程通知设备令牌?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47386207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Remote Notification Device Token in Swift 4?
提问by Sandip Gill
I am using this code to get the Notification Center Device token.
我正在使用此代码获取通知中心设备令牌。
It was working in Swift 3 but not working in Swift 4. What changed?
它在 Swift 3 中有效,但在 Swift 4 中无效。发生了什么变化?
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {import Foundation
extension Data {
var hexString: String {
let hexString = map { String(format: "%02.2hhx", func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.hexString
print(deviceTokenString)
}
) }.joined()
return hexString
}
}
+ String(format: "%02X", )})
print(deviceTokenString)
}
回答by Ahmad F
Assuming that you already checked that everything has been setup right, based on your code, it seems that it should works fine, all you have to do is to change the format to %02.2hhx
instead of %02X
to get the appropriate hex string. Thus you should get a valid one.
假设您已经检查过所有设置是否正确,根据您的代码,它似乎应该可以正常工作,您所要做的就是将格式更改为%02.2hhx
而不是%02X
获取适当的十六进制字符串。因此你应该得到一个有效的。
As a good practice, you could add data extensioninto your project for getting the string:
作为一种好的做法,您可以将数据扩展添加到您的项目中以获取字符串:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.alert,UIUserNotificationType.badge, UIUserNotificationType.sound]
let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
application.registerForRemoteNotifications()
return true
}
Usage:
用法:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("i am not available in simulator :( \(error)")
}
) }.joined()
print(token)
}
回答by Nikunj Kumbhani
Working Code for getting deviceToken in - iOS 11 or greater, Swift 4 | Swift 5
用于获取 deviceToken 的工作代码 - iOS 11 或更高版本,Swift 4 | 斯威夫特 5
Request user permission
请求用户权限
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
let tokenChars = (deviceToken as NSData).bytes.bindMemory(to: CChar.self, capacity: deviceToken.count)
var tokenString = ""
for i in 0..<deviceToken.count {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("tokenString: \(tokenString)")
}
Getting device token
获取设备令牌
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Convert token to string
let deviceTokenString = deviceToken.reduce("", {func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.reduce("", { func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print("Device Token = \(token)")
}
+ String(format: "%02X", )})
print("Device Token : ",token)
}
+ String(format: "%02X", )})
}
In case of error
出错时
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let hexString = deviceToken.map { String(format: "%02.2hhx", func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken
devicetoken: Data)
{
let deviceTokenString = deviceToken.hexString
print(deviceTokenString)
}
) }.joined()
print(hexString)
}
回答by Uma Madhavi
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
userDefault.set(fcmToken, forKey: "fcmToken")
}
回答by Ganesh Manickam
To get device token
获取设备令牌
let deviceIds = UIDevice.current.identifierForVendor!.uuidString
Hope this will help you
希望能帮到你
回答by Vinoth Vino
You can get the device token like this:
您可以像这样获取设备令牌:
##代码##回答by Pankaj Jangid
Use Below code for getting Device token:
使用以下代码获取设备令牌:
##代码##回答by Sazzad Hissain Khan
Swift 5
斯威夫特 5
You can use like,
你可以使用像,
##代码##回答by Dhaval Gevariya
You can use this both function for getting Device Token & FCM Token:
您可以使用这两个函数来获取设备令牌和 FCM 令牌:
For device token use this:
对于设备令牌使用这个:
##代码##For FCM token use this:
对于 FCM 令牌使用这个:
##代码##For UUID use this:
对于 UUID 使用这个:
##代码##