Best Practices
This guide outlines recommended practices for implementing push notifications with Dialect's API to ensure reliable delivery and optimal user experience.
Token Lifecycle Management​
Understanding Token Types​
Dialect's push notification system involves two types of tokens:
-
Dialect Authentication Token
- Obtained through wallet signature verification
- Used to authenticate API requests
- Has a 1-year expiration by default
- Securely identifies the user's wallet
-
Firebase Cloud Messaging (FCM) Token
- Provided by the Firebase Cloud Messaging SDK
- Identifies a specific app instance on a device
- Can change when the app is reinstalled or when Firebase rotates tokens
- Used to route push notifications to the correct device
Managing Authentication Tokens​
Store the Dialect authentication token securely on the device. This token should be used for all authenticated API calls to Dialect's services. While the token has a 1-year expiration, we recommend implementing proper error handling for cases where the token might become invalid.
Handling FCM Token Changes​
Firebase may rotate FCM tokens periodically for security reasons. Additionally, tokens will change when:
- The user reinstalls your application
- The user clears app data
- The device is restored from a backup
To handle these scenarios, we recommend calling the
/subscribe
endpoint whenever your app starts,
ensuring the FCM token is always up-to-date in our system.
For more information you can also check the Firebase docs.
Application Lifecycle Events​
App Start​
When your application starts, we recommend:
- Checking if you have a stored Dialect authentication token
- If yes, verifying it's still valid using the
/auth
endpoint - Retrieving the current FCM token from Firebase
- Calling the subscribe endpoint to ensure the token is registered
Adding or Removing Wallets​
If your application supports multiple wallets:
-
When adding a new wallet:
- Authenticate the wallet
- Subscribe it to push notifications immediately
-
When removing a wallet:
- Unsubscribe it from push notifications to prevent unwanted notifications
This ensures users only receive notifications relevant to their active wallets.
Error Handling Strategies​
Implement robust error handling for your push notification implementation:
-
Network Errors:
- Implement retry logic with exponential backoff
- Queue subscription attempts for when connectivity is restored
-
Authentication Failures:
- Prompt the user to re-authenticate when tokens expire
- Provide clear error messages
-
Device-Specific Issues:
- Handle cases where FCM is not available (e.g., devices without Google services)
- Provide fallback notification mechanisms where appropriate
Testing Your Implementation​
Before deploying to production:
- Test the full notification flow from authentication to delivery
- Verify notifications are received in both foreground and background states
- Test across different device types and OS versions
- Simulate token expiration and app reinstallation scenarios
By following these best practices, you can ensure a reliable and user-friendly push notification experience in your application.