Mete
Mete

Reputation: 5625

iOS Firebase Push Notifications received to first token only, after token changes no longer received

edit: Updating question with new information.

So there's a number of questions out there about Firebase notifications not being received but I've not found one quite like this.

I've recently switched over from using the old p12 APNs certificates to the glorious new p8, and uploaded it to all of my projects on Firebase v4.0.0.

What I'm seeing is, when I do a fresh install of any of my projects, I can send & receive push notifications fine. But after some time, the token changes - and it just stops working - Firebase says "message sent successfully" but no message is received.

Weirdly - my app still receives push notifications to the previous Firebase token, while the new one reported by Firebase isn't working.

Following the advice at Debugging Firebase Cloud Messaging on iOS, I happily debugged the morning away:

  1. Are there any error messages coming back from my Postman firebase attempts? Nope, success:1 every time
  2. Am I getting pushes with app in the background, foreground or neither? Neither.
  3. Are my AppDelegate remote notification registration attempts working successfully? Yes.
  4. Can I directly send a message over APNs, using the new .p8 file? Yes (thanks to this). Messages are being received when I send them directly over APNs just fine!

In the Firebase console, if I send a message to all devices in a project, I receive the message to all devices. But if I try to limit it to my debug device via its FCM token, I get nothing.

So there's one last link that's just not working - appearing to work perfectly for new installs and then bombing after some amount of time - the FCM to APNs link. But how would I debug it?

Upvotes: 1

Views: 924

Answers (1)

Abhishek Biswas
Abhishek Biswas

Reputation: 1263

Here in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

...
NotificationCenter.default.addObserver(self,
                                               selector: #selector(self.tokenRefreshNotification),
                                               name: .firInstanceIDTokenRefresh,
                                               object: nil)
        return true
}

func tokenRefreshNotification(_ notification: Notification) {
        if let refreshedToken = FIRInstanceID.instanceID().token() {
            print("InstanceID token: \(refreshedToken)")
            // Here you get the refreshed token
            // here you can connect to fcm and do subscribe to notifications
        }
    }

Hopefully it will solve the problem.

Upvotes: 1

Related Questions