Android unique device identifier to use with google's firebase token generator

10,329

Solution 1

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:

  • App deletes Instance ID
  • App is restored on a new device User
  • uninstalls/reinstall the app
  • User clears app data

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.

  • No device ID is necessary, much safer for user.
  • Sends 1 FCM to a bad token, but not again after that
  • If Firebase Team changes its errors or error messages, you will have to change the function or this will no longer work.

See this answer for more details: Handling refresh tokens for FCM device groups

Solution 2

In class you are extending "FirebaseInstanceIdService" in method "onTokenRefresh" you can get it like :

FirebaseInstanceId.getInstance().getId()
Share:
10,329
roostaamir
Author by

roostaamir

Updated on June 18, 2022

Comments

  • roostaamir
    roostaamir over 1 year

    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?