I am creating an application that uses goolg's firebase as its push notification service. I am just using the push notifications server and DON'T WANT TO use its other services (such as database, authentication and etc).
In my application, a user that signs up can have multiple devices and thus multiple firebase tokens. The problem occurs when onRefreshToken
is called. I should know which device the user is using and update that specific device's token in my database. So I should know a unique identifier for each device that does not change in device's lifetime.
I was wondering if there are any better ways to tackle this problem and if not, I am so confused about an android unique ID. The two options are Settings.Secure.ANDROID_ID
which may have some issues (so I am not sure if I should use it) and TelephonyManger.getDeviceId()
which return null sometimes and for devices that have no sim slots (such as tablets).
So what do you suggest me to do?
ANDROID_ID is the preferred device identifier. ANDROID_ID is perfectly reliable on versions of Android <=2.1 or >=2.3.
* getDeviceId() returns the unique device ID. * For example,the IMEI for GSM and the MEID or ESN for CDMA phones. * getSubscriberId() returns the unique subscriber ID, * For example, the IMSI for a GSM phone.
In an iOS or Firebase app, device ID gets its value from the app-instance ID, which identifies a unique installation of the app. Device ID is one of the available reporting identity spaces.
This answer has 2 solutions:
1) Using ANDROID_ID as a Device Identifier.
2) Using cloud functions and FCM to determine a bad token, after 1st message sent to it.
1) Try Settings.Secure.ANDROID_ID to get the Device ID.
String deviceAppUID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
if (deviceAppUID!=null) {
setFirebaseTokenInServer(deviceAppUID, FirebaseDeviceToken);}
Android is changing ANDROID_ID in 8.0 (API 26), so that it will apply only to the combination of specific app, user, and deviceId. When that happens, you aren't really getting a Device ID that is ALWAYS true for every app, but rather, you are getting a Device ID for your specific App, with its specific signing key, on your specific devise. You can uninstall the app, and reinstall the app, and the ANDROID_ID will be the same as it was before.
That makes it safer for the user, but would still allow you to persist the Firebase Device Tokens in case the registration token changes for these following reasons:
Then in Firebase, you can save these as:
DeviceTokens:
deviceID: firebaseDeviceToken
deviceID: firebaseDeviceToken
2) Using Firebase Cloud Messaging and Firebase Google Cloud Functions to determine a bad token.
If you also use Cloud Functions to send the FCM to each listed deviceToken (as is necessary if multiple devices), you can check for a response after the first notification is sent. If there is an error, and the error returns a message stating the token is bad, you can handle the error and then delete it from the Server.
See this answer for more details: Handling refresh tokens for FCM device groups
In class you are extending "FirebaseInstanceIdService" in method "onTokenRefresh" you can get it like :
FirebaseInstanceId.getInstance().getId()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With