Get your GCM registration number without sending a message

I have a problem with an application using GCM, the scenario is this:

  • application installed
  • the application calls the GCM register method, getting the registration identifier "RID-1"
  • application uninstalled
  • the application is installed again
  • the application calls the GCM register method, again getting the registration identifier "RID-2"

In step 5, I need to get the previous registration ID so that I can update my model.

Limitations:
- I'm trying to do this without using external storage
- I can’t update the model when the server sends a message, this should be done after registration, because a new new profile is created in the application for each new device.

I know that this information is located on Google servers because it is sent to you when you send a message to the old registration ID. For example, if I send a message to "RID-1", in the response I get that the new (canonical) registration identifier is "RID-2" . I need a way to get this information without sending a message.

Let me know if you need more context.


I found some related questions, but the answers are not applicable to this scenario:
GCM Registration Identity Repeat
gcm canonical id should be updated or not
Gcm registration id stability
Google Cloud Messaging - Registration ID Status
Android GCM: how to detect registered canonical identifiers on my own server? Handling Google Cloud Messaging sign-in ID changes on Android
( @eran answered everything )

+8
android push-notification google-cloud-messaging
source share
3 answers

You can specify "dry_run": true in the /send request.

I found that devices do not receive any push notifications with the parameter "dry_run": true , and the server receives a canonical_ids response.

Here is a sample code in Ruby. You may need to install gcm Gem in advance.

$ gem install gcm

ask_canonical_ids.rb

 require 'gcm' require 'json' API_KEY = "YourApiKey" gcm = GCM.new(API_KEY) registration_ids = [ 'OldRegistrationId', ] option = { data: { 'message' => 'Hello Gcm!' }, dry_run: true } response = gcm.send_notification(registration_ids, option) p response[:canonical_ids] 

output $ ruby ask_canonical_ids.rb (formatted)

 [{ :old => "OldRegistrationId", :new => "NewRegistrationId" }] 

Again, your device will not receive any push notifications.

+5
source share

We need to update the registration identifier using Canonical Id (by finding the index position of the array). You can do the following: Ready-made usage code

+1
source share

If you only need that the user should not receive a notification, send a message with parameters that your application is not looking for. You will receive the canonical, and your application will cancel the notification if it does not have the required text and message.

For example, my Cordova plugin requires a message key in the data received from the server. Otherwise, it does not generate a notification.

I know this is a kind of hack, but I think that given the limitations, this will be easiest to achieve.

0
source share

All Articles