无需发送消息即可获取GCM规范注册ID [英] Get GCM canonical registration ID without sending a message

查看:155
本文介绍了无需发送消息即可获取GCM规范注册ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GCM的应用程序出现问题,情况是这样的:


  1. 安装了应用程序

  2. 应用程序调用GCM注册方法获取注册IDRID-1

  3. 应用程序已卸载

  4. 应用程序为再次安装

  5. 应用程序再次调用GCM注册方法获取注册IDRID-2



在步骤5中,我需要获取以前的注册ID,以便更新我的模型。



局限性:

- 我尝试在不使用外部存储的情况下执行此操作

- 服务器发送消息时,我无法更新模型,因为创建了新配置文件,所以应在注册后完成在每个新设备的应用程序中



我知道这些信息位于Google服务器中,因为它会在您向旧注册ID发送消息时发送给您。例如,如果我发送消息给RID-1,则在响应中我得到新的(规范)注册ID为RID-2。我需要的是一种在不发送信息的情况下获取此信息的方法。



让我知道您是否需要更多上下文。






我发现了几个相关的问题,但答案并不适用于此场景:

GCM注册ID重复

应更新gcm canonical id或不

坚持gcm注册ID

< Google Cloud Messaging - 注册ID状态

Android GCM:如何在我自己的服务器中检测注册的规范ID?

处理Android版Google云消息传递中的注册ID更改

(全部通过@eran https://developers.google.com/cloud-messaging/http-server-ref#table1 =noreferrer> dry_run:true 选项 a>在 / send 请求中。



我发现设备不会收到dry_run:true 选项,而服务器获取 canonical_ids 响应。

这是Ruby中的示例代码。您可能需要事先安装 gcm Gem。


$ b

$ gem install gcm $ b

ask_canonical_ids.rb

  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]

输出 $ ruby​​ ask_canonical_ids.rb (格式化)

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

同样,您的设备也不会收到任何推送通知。 / p>

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-1"
  3. the app is uninstalled
  4. the app is installed again
  5. the app calls the GCM register method again getting the registration id "RID-2"

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

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

I know that this information is in 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 id is "RID-2". What I need is a way to get this information without sending a message.

Let me know if you need more context.


I found several related questions but the answers doesn't apply to this scenario:
Registration ID duplication for GCM
gcm canonical id should be updated or not
Persistance of gcm registration id
Google Cloud Messaging - Registration ID status
Android GCM: How to detect registered canonical ids in my own server?
Handling registration ID changes in Google Cloud Messaging on Android
(all answered by @eran)

解决方案

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.

这篇关于无需发送消息即可获取GCM规范注册ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆