gpt4 book ai didi

ios - 我想将 FCMToken 发送到 IOS 中的 URL

转载 作者:行者123 更新时间:2023-11-29 05:59:27 25 4
gpt4 key购买 nike

我将应用程序开发为 webview。并成功配置 firebase 推送通知。消息传递良好。

但现在我想将设备 token 放入 URL 中。通过它我可以根据 token 发送消息。

我在 Appdelegate.swift 中编写此代码

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map {String (format: "%02.2hhx", $0)}.joined()
print("Token : \(token)")
let preferences = UserDefaults.standard
preferences.setValue(token, forKey: "token")
preferences.synchronize()
}

//Changes

func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError
error: Error) {
// Try again later.
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}

// Print full message.
print(userInfo)

completionHandler()
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")

//Saving fcmToken to pass to the url

//Bhaskar Changes

let preferences = UserDefaults.standard
preferences.setValue(fcmToken, forKey: "token")
preferences.synchronize()
let token = Messaging.messaging().fcmToken
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Message Data", remoteMessage.appData)
}

以及 Viewcontroller.swift 中的这段代码

override func viewDidLoad() {
super.viewDidLoad()

//Retrieving the fcmToken
let prefs = UserDefaults.standard
let token = prefs.string(forKey: "token")
//?token= \(token as Optional)&device=ios
print("Token accessed : \(String(describing: token))")
webView.load(URLRequest(url: URL(string: "https://instaglamexpress.com/app/customer/?device=ios&token=\(String(describing: token))")!))

}

但是每次我运行程序时。 token 没有存储到 token 变量中。它显示“nil”。

我是否遵循了正确的程序??

请帮我提供实际的解决方案。

最佳答案

也许你忘记添加Messaging.messaging().delegate = self,这样func communications(_ messages: Messaging, didReceiveRegistrationToken fcmToken: String)将永远不会已调用。

关于ios - 我想将 FCMToken 发送到 IOS 中的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54829688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com