tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.11k stars 1.44k forks source link

registerDevice returns with error #2368

Closed vincentneo closed 1 year ago

vincentneo commented 1 year ago

I've realise that registerDevice recently is returning me an error, with code 400, and the message is TOKEN_WAS_INVALIDATED.

Device with observed issue is Apple Watch, watchOS 9.3.1. device_token type is deviceTokenApplePush. other_user_ids is sent with an empty array.

Notifications are not being received.

What does that mean, and what can I do to remedy that?

levlam commented 1 year ago

The error is returned when APNS recently rejected provided token as invalid. When it is received, the app needs to ask OS for a new push notification token.

vincentneo commented 1 year ago

@levlam but with every app launch I call WKApplication.shared().registerForRemoteNotifications() which in turn calls the delegate function didRegisterForRemoteNotifications(withDeviceToken deviceToken: Data).

That function provides a (presumably new device token) after which I call TDLib's registerDevice with that token.

The issue happens even after a fresh relaunch, so are there any other possibilities where APNS will reject the token?

vincentneo commented 1 year ago

By the way, didFailToRegisterForRemoteNotificationsWithError https://developer.apple.com/documentation/watchkit/wkextensiondelegate/3141923-didfailtoregisterforremotenotifi

was never called either.

levlam commented 1 year ago

registerForRemoteNotifications each time returns the same already invalid token and the token must be refreshed by calling unregisterForRemoteNotifications, waiting a second, and then calling registerForRemoteNotifications. The server knows that the provided token is invalid, because APNS has returned an error when the server tried to use the token.

vincentneo commented 1 year ago

thanks for the advice, will try it out.

vincentneo commented 1 year ago

Yup that seems to have solved it!

Once again thank you very much @levlam!