Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GCM get instance id bug

Tags:

android

When I try to get InstanceID from the GCM per the doc, Android Studio complains about missing context parameter. The documentation doesn't mention that. How do I resolve this? Do I simply pass context as such:

String iid = InstanceID.getInstance(this).getID();
like image 402
The_Martian Avatar asked May 16 '26 11:05

The_Martian


2 Answers

Yes, as per the InstanceID Javadoc, you must pass a Context to InstanceID.getInstance() - be that your Activity or another context.

like image 143
ianhanniballake Avatar answered May 19 '26 02:05

ianhanniballake


Try this

try {
            InstanceID instanceID = InstanceID
                    .getInstance(getApplicationContext());
            token = instanceID.getToken(ApplicationConstants.SENDER_ID,
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        } catch (IOException e) {
            e.printStackTrace();
        }       
like image 43
Bhavinkumar Patel Avatar answered May 19 '26 04:05

Bhavinkumar Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!