Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS FCM token is NotRegistered after deleting and reinstalling APP #160

Open
sagun-gautam opened this issue Jun 20, 2024 · 8 comments
Open

Comments

@sagun-gautam
Copy link

sagun-gautam commented Jun 20, 2024

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 sagun-gautam changed the title iOS FCM token is NotRegistered after deleting and reinstalling APP from test flight iOS FCM token is NotRegistered after deleting and reinstalling APP Jun 20, 2024
@ThiagoTesche
Copy link

Exactly, the same problem here! waiting for solution

@leonardoMoliveira
Copy link

Same problem here 😢

@sagun-gautam
Copy link
Author

Anyone got the solution?

@leonardoMoliveira
Copy link

leonardoMoliveira commented Jun 27, 2024

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

@knurlann
Copy link

The same problem(

@leonardoMoliveira
Copy link

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

@sagun-gautam
Copy link
Author

sagun-gautam commented Jul 26, 2024

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)
} `

@leonardoMoliveira
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants