Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 941
roostaamir Avatar asked Jul 09 '16 05:07

roostaamir


People also ask

What is the best identifier to use to uniquely identify a device?

ANDROID_ID is the preferred device identifier. ANDROID_ID is perfectly reliable on versions of Android <=2.1 or >=2.3.

How do I find the unique ID of my Android phone?

* 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.

How do I find my device ID for Firebase?

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.


2 Answers

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

like image 114
Jeff Padgett Avatar answered Oct 23 '22 18:10

Jeff Padgett


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

FirebaseInstanceId.getInstance().getId()
like image 41
ergunkocak Avatar answered Oct 23 '22 19:10

ergunkocak