Get GCM canonical registration ID without sending a message

后端 未结 3 541
眼角桃花
眼角桃花 2021-02-06 05:12

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

  1. the app is installed
  2. the app calls the GCM register method getting the registration id \"RID
相关标签:
3条回答
  • 2021-02-06 05:37

    We need to update registration id with Canonical Id( By finding index position of array). You may Follow this working Ready use Code

    0 讨论(0)
  • 2021-02-06 05:40

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

    I found that devices do not receive any push notifications with "dry_run": true option, while a server get canonical_ids response.

    Here is a sample code in Ruby. You may have to install gcm Gem beforehand.

    $ 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 of $ ruby ask_canonical_ids.rb (formatted)

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

    Again, your device will not receive any push notifications.

    0 讨论(0)
  • 2021-02-06 05:53

    If all you need is that the user should not get a notification, send a message with parameters that your application is not looking for. You will get the canonical and your app will discard the notification if it does not have the mandatory text and message.

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

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

    0 讨论(0)
提交回复
热议问题