I want to send notifications, but only to authenticated users. This means, if my token updates (potential reasons: app is restored on a new device, user uninstalls/reinstall the app or user clears app data) my user will also not be authenticated.
Thus, whenever my user tries to login i update the token, which i receive via this method, for my backend:
Messaging.messaging().token { token, error in
// ...
}
When the user logs out i delete the token, so why would i implement this method here:
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
}
However, if i do not implement it, i receive this:
9.5.0 - [FirebaseMessaging][I-FCM002023] The object <Name.Delegate: 0x2827c0650> does not respond to -messaging:didReceiveRegistrationToken:. Please implement -messaging:didReceiveRegistrationToken: to be provided with an FCM token.
In this function you must Save your device " Token " or " refresh token " as Local:
"didReceiveRegistrationToken"
When the user logged in you must register the push-token in the backend
When the user logged out you must clear the push-token in the backend
You must after login set a key value in "KeyChain" such as:
"userId" : "x"
"For the users that before login, cleared or uninstalled the app" :
In each launch you must check your app user is login or not?
if " NO " (doesn't have the token):
You must clear the push-token with "userId -> x" that you saved on keychain. (request it to the backend)
Albeit the new push-token is different with the previouse push-token.
if " YES " (user had backend token)
you must save the new "push-token" or "refresh-push-token" from below function:
"didReceiveRegistrationToken"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.