12

I found this solution and I think it is not the ID which is required for notification kindly tell me by some samples:

import android.provider.Settings.Secure;

private String androidIdd = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);

I guess I don't need an android id. I just want to get the device unique ID for Firebase Notification or GCM.

Anže Mur
  • 1,545
  • 2
  • 18
  • 37
Ahsan Malik
  • 399
  • 2
  • 3
  • 13
  • Welcome to StackOverflow, please read the guides on [How to ask](https://stackoverflow.com/help/asking) in order to better understand your question and provide you with even better answers. – Paul Karam Jun 19 '17 at 08:03
  • Thnx for guiding me paul karam – Ahsan Malik Jun 19 '17 at 08:25
  • It's pretty clear from the documentation. Just follow the step by step guide and you'll be fine https://firebase.google.com/docs/cloud-messaging/android/client – Qw4z1 Jun 19 '17 at 09:21

1 Answers1

15

Instead of using some third party tutorials I would prefer to take a look into the official Firebase Cloud Messaging documentation.

If you have set up everything correctly you can just scroll to the topic

Retrieve the current registration token

When you need to retrieve the current token, call FirebaseInstanceId.getInstance().getToken(). This method returns null if the token has not yet been generated.

In a nutshell: Call FirebaseInstanceId.getInstance().getToken() to reveive the current device token.

EDIT:

The command FirebaseInstanceId.getInstance().getToken() is deprecated. The same documentation mentions a new approach as below:

FirebaseInstanceId.getInstance().getInstanceId()
        .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
            @Override
            public void onComplete(@NonNull Task<InstanceIdResult> task) {
                if (!task.isSuccessful()) {
                    Log.w(TAG, "getInstanceId failed", task.getException());
                    return;
                }

                // Get new Instance ID token
                String token = task.getResult().getToken();

            }
        });
Community
  • 1
  • 1
StefMa
  • 3,344
  • 4
  • 27
  • 48