0

I have been working on implementing GCM in an application and based on Google's examples have been able to get it to work. However, when I try to use my own sender ID and scope, I get back an InvalidRegistration error when I try to send a notification. For example, this works:

token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

Where as this does not, I have already double checked the sender ID from the project, in fact, there is only one project in my developer console right now, so there's no way I could be using an incorrect one:

token = instanceID.getToken(getString(R.string.my_sender_id), SubscriptionHelper.INSTANCE_SCOPE, null);

Am I missing a key component here? Why does it work with the default sender ID, but not with the Sender ID I've obtained from the developer console?

akousmata
  • 1,005
  • 14
  • 34
  • What is `SubscriptionHelper.INSTANCE_SCOPE`? That scope doesn't look like the one needed for GCM (`GoogleCloudMessaging.INSTANCE_ID_SCOPE`)? – ianhanniballake Jun 09 '15 at 19:02
  • Check that the token sent to your server is complete (may want to log from both client and server to check this). These errors can happen if the token was truncated or altered. – Koh Jun 09 '15 at 19:16
  • @ianhanniballake That is a custom string. Per some of the examples I've found: `e.g. communicating using GCM, but you can use any URL-safe characters up to a maximum of 1000, or you can also leave it blank.` Check this Q/A http://stackoverflow.com/questions/30653130/what-is-authorizedentity-cant-find-gcm-defaultsenderid-in-own-app @Koh I am getting the token from the server, then manually copy/paste into a test application for sending. I have double checked it. – akousmata Jun 09 '15 at 19:28

1 Answers1

2

Per the GCM Android Client guide, your InstanceID token call should look like

InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(
    getString(R.string.gcm_defaultSenderId),
    GoogleCloudMessaging.INSTANCE_ID_SCOPE, 
    null);

Note that you must use GoogleCloudMessaging.INSTANCE_ID_SCOPE as the second parameter - InstanceID is more general than just GCM, hence why it is a parameter that can take any String, but GCM specifically requires that authorization scope.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Yes, I used that guide for my original prototype, then when implementing in my own app, I wanted to use my own sender ID which is where I came across, this documentation: https://developers.google.com/instance-id/guides/android-implementation It uses a hard-coded value for GCM rather than the built in constant which is where I think my confusion came from. Thanks you for the clarification. – akousmata Jun 09 '15 at 21:07
  • I am using the above code but I still have this same error.. Could you take a look? http://stackoverflow.com/questions/35896634/android-gives-me-a-gcm-token-but-gae-says-it-is-not-registered-when-sending-mess – Micro Mar 09 '16 at 16:53