
IOS
Swiftimport UIKitimport UserNotificationsclass AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // 注册远程通知 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in if granted { DispatchQueue.mAIn.async { application.registerForRemoteNotifications() } } } return true } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined() print("IOS 设备令牌:\(token)") } func application(_ application: UIApplication, didFAIlToRegisterForRemoteNotificationsWithError error: Error) { print("注册远程通知失败:\(error.localizedDescription)") } }Android 设备令牌示例代码:在 Android 开发中,获取设备令牌可以使用 Google 提供的 Firebase Cloud Messaging(FCM)服务。以下是一个简单的示例代码,演示了如何获取 Android 设备令牌:Javaimport Android.util.Log;import com.Google.firebase.iid.FirebaseInstanceId;import com.Google.firebase.iid.FirebaseInstanceIdService;public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIDService"; @Override public void onTokenRefresh() { // 获取设备令牌 String token = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Android 设备令牌: " + token); }}IOS 和 Android 设备令牌在移动应用开发中起着重要的作用,用于唯一标识用户设备。IOS 设备令牌长度固定为 64 个字符的十六进制字符串,而 Android 设备令牌长度可变,通常为 140 个字符左右。开发者可以根据需要使用相应的示例代码获取设备令牌,并将其应用于推送通知、设备识别和用户身份验证等功能中。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号