如果用户当前的 FCM 令牌不再有效,我该如何更新他们?
How can I update users FCM token if their current one no longer works?
我发现我的应用程序存在问题,某些用户暂时停止接收推送通知。他们与账户相关联的 FCM 令牌似乎已经过期或需要一个新令牌。我通过删除设备上的应用程序进行测试,并在 Xcode 中获得了一个新的 fcm 令牌。
我从 Xcode 控制台复制了该 FCM 令牌并手动将其替换为我在 Firebase 数据库中的令牌,然后我能够成功接收推送通知。
我的问题是,当当前用户重新登录/或再次打开应用程序时,是否可以更新当前用户的 FCM 令牌,以便他们可以成功接收推送通知?
这是我的应用委托
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
attemptRegisterForNotifications(application: application)
Messaging.messaging().delegate = self
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Registered for notifications", deviceToken)
Messaging.messaging().apnsToken = deviceToken
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Registered with FCM with token:", fcmToken)
}
// Listen for user notifcations
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
private func attemptRegisterForNotifications(application: UIApplication) {
print("Attempting to register APNS...")
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, . badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
if let error = error {
print("Failed to request auth:", error)
return
}
if granted {
print("Auth granted.")
} else {
print("Auth denied")
}
}
application.registerForRemoteNotifications()
}
您需要在每次应用启动时更新服务器数据库中的用户令牌,以便始终拥有最新的令牌。
您不需要手动 "renew" 设备令牌(事实上,没有办法强制这样做)。相反,您应该期望设备令牌可以随时更改。因为您的应用程序可能会意外获得新令牌,所以您应该在每次应用程序启动时为用户记录该令牌。旧令牌将不再有效,您的服务器代码应检查是否有故障,以便知道何时删除它。
您想使用 Firebase Messaging 委托 FIRMessagingDelegate
来处理 FCM 令牌的更新。理想情况下,您希望在应用程序启动时处理此问题,因此请考虑在 App Delegate 中执行此操作:
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Messaging.messaging().delegate = self
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
if let userId = Auth.auth().currentUser?.uid { // user is signed in
Firestore.firestore().collection("userProfiles").document(userId).updateData(["fcmToken": fcmToken]) { (error) in
if let error = error {
print(error)
}
}
}
}
}
您必须自己管理与令牌关联的用户。当用户登录时,您应该将令牌与用户的 ID 相关联,当用户退出时,您应该删除该关联。
我发现我的应用程序存在问题,某些用户暂时停止接收推送通知。他们与账户相关联的 FCM 令牌似乎已经过期或需要一个新令牌。我通过删除设备上的应用程序进行测试,并在 Xcode 中获得了一个新的 fcm 令牌。
我从 Xcode 控制台复制了该 FCM 令牌并手动将其替换为我在 Firebase 数据库中的令牌,然后我能够成功接收推送通知。
我的问题是,当当前用户重新登录/或再次打开应用程序时,是否可以更新当前用户的 FCM 令牌,以便他们可以成功接收推送通知?
这是我的应用委托
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
attemptRegisterForNotifications(application: application)
Messaging.messaging().delegate = self
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Registered for notifications", deviceToken)
Messaging.messaging().apnsToken = deviceToken
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Registered with FCM with token:", fcmToken)
}
// Listen for user notifcations
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
private func attemptRegisterForNotifications(application: UIApplication) {
print("Attempting to register APNS...")
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, . badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (granted, error) in
if let error = error {
print("Failed to request auth:", error)
return
}
if granted {
print("Auth granted.")
} else {
print("Auth denied")
}
}
application.registerForRemoteNotifications()
}
您需要在每次应用启动时更新服务器数据库中的用户令牌,以便始终拥有最新的令牌。
您不需要手动 "renew" 设备令牌(事实上,没有办法强制这样做)。相反,您应该期望设备令牌可以随时更改。因为您的应用程序可能会意外获得新令牌,所以您应该在每次应用程序启动时为用户记录该令牌。旧令牌将不再有效,您的服务器代码应检查是否有故障,以便知道何时删除它。
您想使用 Firebase Messaging 委托 FIRMessagingDelegate
来处理 FCM 令牌的更新。理想情况下,您希望在应用程序启动时处理此问题,因此请考虑在 App Delegate 中执行此操作:
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Messaging.messaging().delegate = self
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
if let userId = Auth.auth().currentUser?.uid { // user is signed in
Firestore.firestore().collection("userProfiles").document(userId).updateData(["fcmToken": fcmToken]) { (error) in
if let error = error {
print(error)
}
}
}
}
}
您必须自己管理与令牌关联的用户。当用户登录时,您应该将令牌与用户的 ID 相关联,当用户退出时,您应该删除该关联。