fcm icon indicating copy to clipboard operation
fcm copied to clipboard

iOS FCM token is NotRegistered after deleting and reinstalling APP

Open sagun-gautam opened this issue 1 year ago • 8 comments

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:

  1. Your app should have push notification enabled.
  2. Delete your app.
  3. Again Immediately, install the app from app store
  4. Enable push notification.
  5. Send push notification to that user FCM token.
  6. 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

sagun-gautam avatar Jun 20 '24 00:06 sagun-gautam

Exactly, the same problem here! waiting for solution

ThiagoTesche avatar Jun 21 '24 17:06 ThiagoTesche

Same problem here 😢

leonardoMoliveira avatar Jun 21 '24 18:06 leonardoMoliveira

Anyone got the solution?

sagun-gautam avatar Jun 26 '24 23:06 sagun-gautam

@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.

leonardoMoliveira avatar Jun 27 '24 12:06 leonardoMoliveira

The same problem(

knurlann avatar Jul 26 '24 07:07 knurlann

@knurlann @sagun-gautam can you guys paste here your AppDelegate.swift?

leonardoMoliveira avatar Jul 26 '24 12:07 leonardoMoliveira

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 avatar Jul 26 '24 12:07 sagun-gautam

@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 🙏🏼

leonardoMoliveira avatar Jul 26 '24 13:07 leonardoMoliveira