When to Refresh Push Notification Tokens

by mmyoji

2 min read

tl;dr

  • iOS: basically ask token to APNs and notify to backend service
  • FCM: notify backend service whenever the token is refreshed because FCM token refresh timing is clarified

iOS

Local and Remote Notification Programming Guide: Configuring Remote Notification Support

Never cache device tokens in your app; instead, get them from the system when you need them. APNs issues a new device token to your app when certain events happen. The device token is guaranteed to be different, for example, when a user restores a device from a backup, when the user installs your app on a new device, and when the user reinstalls the operating system. Fetching the token, rather than relying on a cache, ensures that you have the current device token needed for your provider to communicate with APNs. When you attempt to fetch a device token but it has not changed, the fetch method returns quickly.

In APNs, you don't know when to expire / refresh token, always fetch token and sync with backend service.

Other References

iOS 9からAPNsデバイストークンがアプリインストールの度に変わるようになったようです - Qiita (JP)

Android (FCM)

GCM has already been deprecated at May, 2019.

Set up a Firebase Cloud Messaging client app on Android | Firebase

The registration token may change when:

> The app deletes Instance ID > The app is restored on a new device > The user uninstalls/reinstall the app > The user clears app data.