iOS FCM token is NotRegistered after deleting and reinstalling APP
Describe the bug
The token generated by FCM.getToken() after deleting and installing the app from store returns shows 'UnRegistered' in FCM Rest API response.
To Reproduce Steps to reproduce the behavior:
- Your app should have push notification enabled.
- Delete your app.
- Again Immediately, install the app from app store
- Enable push notification.
- Send push notification to that user FCM token.
- Push notification is not received & Firebase API returns 'NotRegistered'.
Expected behavior FCM token should be valid, I can see the FCM token is stored is valid & changed from last token.
Screenshots If applicable, add screenshots to help explain your problem.
Smartphone (please complete the following information):
- Device: iPhone
- OS: iOS 17.5.1
Additional context Some time the token generated after reinstalling the app is same in iOS
Exactly, the same problem here! waiting for solution
Same problem here 😢
Anyone got the solution?
@sagun-gautam A workaround for me was create and endpoint in our backend that validates the push token by sending a data-only push notification and verifying the status code !== 404, then it returns back to the app. If is an invalid token, app calls the function FCM.refreshToken(), it generates a new token (this token may be invalid too, but it works in most cases). It occurs always in the app opening, so the token is always verified. Its not a 100% solution, but it helped a lot. Still waiting for a definitive solution.
The same problem(
@knurlann @sagun-gautam can you guys paste here your AppDelegate.swift?
Hi there, so I upgraded to capacitor 6 and changed code according to this guide and it’s started working fine for me: https://capacitorjs.com/docs/guides/push-notifications-firebase
update podfile:
target 'App' do capacitor_pods pod 'Firebase/Messaging'
update AppDelegate.swift : `import UIKit import Capacitor import Firebase
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FirebaseApp.configure() return true }
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken Messaging.messaging().token(completion: { (token, error) in if let error = error { NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error) } else if let token = token { NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token) } }) }
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error) } `
@sagun-gautam that's totally make sense, I just figured out yesterday that the code in my AppDelegate was:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}
so I changed it to the same as you pasted there and it started to work fine 🙏🏼