【问题标题】:GCM Token validationGCM 令牌验证
【发布时间】:2013-09-25 05:50:31
【问题描述】:

我在验证 GCM 令牌时有点困惑。我正在使用 Sencha 框架在跨平台应用程序中工作,而我的服务器端使用 Java。我对如何验证注册 ID(GCM 令牌)有疑问?是否有任何特定的 API 来验证 GCM 令牌?你能指导我如何在客户端或服务器端处理这个问题吗?我已经在服务器端做了注册部分,用户可以在数据库中注册他们的 GCM 令牌。现在我需要验证这个注册令牌。

每 2 周注销一次应用程序是否是一个好方法?

【问题讨论】:

    标签: java android sencha-touch push-notification google-cloud-messaging


    【解决方案1】:

    您在客户端注册到 GCM 并从 GCM 取消注册。这是您从 Google 获得注册 ID 的地方。

    一旦您拥有了注册 ID,您应该认为它在以下日期之前有效:

    1. 您将带有注册 ID 的消息发送到 Google 的 GCM 服务器并收到 NotRegistered 或 InvalidRegistration 错误。在这些情况下,您应该从数据库中删除注册 ID。

    2. 您将带有注册 ID 的消息发送到 Google 的 GCM 服务器并获得成功的响应,但响应中包含规范的注册 ID。在这种情况下,您应该将注册 ID 替换为规范注册 ID。

    3. 应用明确从 GCM 取消注册,并通知服务器,在这种情况下,您应该从数据库中删除注册 ID。

    我认为每两周注销一次应用程序没有任何意义。谷歌的代码示例只有在安装了新版本后才重新注册该应用程序,即使这样,他们也不会在重新注册之前取消注册。

    【讨论】:

    • 是的,这就是确切的解决方案。获取规范的 reg id 并替换为旧的。
    • 你能解释一下我什么时候会得到规范的 id,因为当前的注册 id 有效期为 7 天。现在我正在跟踪我的服务器中的规范 id
    • 当前注册ID 7天无效。 Google 可能会决定不时刷新注册 ID。这就是为什么他们发布了一个演示应用程序,认为注册 ID 在 7 天后无效,但上次我检查他们从演示中删除了该条件。如果您注册到 GCM 并获得一个新的注册 ID,您应该将其发送到您的服务器,以更新数据库。如果您不这样做,服务器可能会在 Google 的响应中获得一个规范的注册 ID,然后修改数据库。
    【解决方案2】:

    使用 canonicalID 解决 GCM 验证的解决方案 link

     private void asyncSend(List<String> partialDevices) {
        // make a copy
        final List<String> devices = new ArrayList<String>(partialDevices);
        threadPool.execute(new Runnable() {
    
          public void run() {
            Message message = new Message.Builder().build();
            MulticastResult multicastResult;
            try {
              multicastResult = sender.send(message, devices, 5);
            } catch (IOException e) {
              logger.log(Level.SEVERE, "Error posting messages", e);
              return;
            }
            List<Result> results = multicastResult.getResults();
            // analyze the results
            for (int i = 0; i < devices.size(); i++) {
              String regId = devices.get(i);
              Result result = results.get(i);
              String messageId = result.getMessageId();
              if (messageId != null) {
                logger.fine("Succesfully sent message to device: " + regId +
                    "; messageId = " + messageId);
                String canonicalRegId = result.getCanonicalRegistrationId();
                if (canonicalRegId != null) {
                  // same device has more than on registration id: update it
                  logger.info("canonicalRegId " + canonicalRegId);
                  Datastore.updateRegistration(regId, canonicalRegId);
                }
              } else {
                String error = result.getErrorCodeName();
                if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                  // application has been removed from device - unregister it
                  logger.info("Unregistered device: " + regId);
                  Datastore.unregister(regId);
                } else {
                  logger.severe("Error sending message to " + regId + ": " + error);
                }
              }
            }
          }});
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-04
      • 2021-09-30
      • 2019-06-23
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      相关资源
      最近更新 更多