【问题标题】:How to send message to notification key with GCM without an app server如何在没有应用服务器的情况下使用 GCM 向通知键发送消息
【发布时间】:2016-06-08 04:33:40
【问题描述】:

我在Android上关注Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401管理本地设备组并成功获得了通知密钥,但是当我向通知密钥发送消息时,我再也没有收到消息。 有人做过这个工作吗?

我的发送代码是这样的:

    public void sendMessage(View view) {
    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            try {
                GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(getApplicationContext());
                String to = notificationKey; // the notification key
                AtomicInteger msgId = new AtomicInteger();
                String id = Integer.toString(msgId.incrementAndGet());
                Bundle data = new Bundle();
                data.putString("hello", "world");

                gcm.send(to, id, data);
                Log.e(TAG, "sendMessage done.");
            } catch (Exception ex) {
                Log.e(TAG, ex.toString());
            }
            return null;
        }
    };
    task.execute();
}

【问题讨论】:

  • @greywolf82,你成功发送消息到本地设备组了吗?

标签: android google-cloud-messaging


【解决方案1】:

似乎对 GCM 概念存在误解。应用服务器是integral part of GCM messaging

Google Cloud Messaging (GCM) 的服务器端由两个 组件:

  • Google 提供的 GCM 连接服务器。这些服务器接收消息 从应用服务器并将它们发送到在设备上运行的客户端应用程序。 Google 为 HTTP 和 XMPP 提供连接服务器。
  • 一个应用程序 您必须在您的环境中实现的服务器。这个应用程序 服务器通过选择的 GCM 连接向客户端应用程序发送数据 服务器,使用适当的 XMPP 或 HTTP 协议。

尝试Android GCM Playground 以更好地了解这一点。

这是一个sn-p:

public void sendMessage() {
        String senderId = getString(R.string.gcm_defaultSenderId);
        if (!("".equals(senderId))) {
            String text = upstreamMessageField.getText().toString();
            if (text == "") {
                showToast("Please enter a message to send");
                return;
            }

            // Create the bundle for sending the message.
            Bundle message = new Bundle();
            message.putString(RegistrationConstants.ACTION, RegistrationConstants.UPSTREAM_MESSAGE);
            message.putString(RegistrationConstants.EXTRA_KEY_MESSAGE, text);

            try {
                gcm.send(GcmPlaygroundUtil.getServerUrl(senderId),
                        String.valueOf(System.currentTimeMillis()), message);
                showToast("Message sent successfully");
            } catch (IOException e) {
                Log.e(TAG, "Message failed", e);
                showToast("Upstream FAILED");
            }
        }
    }

【讨论】:

  • 我试图避开应用服务器,并避免在应用源代码中嵌入 api 密钥。我确信可以在没有应用服务器的情况下发送/接收消息,但 API 密钥必须在源代码中。这就是我使用本地设备组的原因。谢谢。
【解决方案2】:

send 方法的 to 字段代表您项目的发送者 ID。您不能使用此方法向实例 ID 令牌(其他设备)发送消息,GCM 目前不支持设备到设备消息传递。

避免在客户端应用中包含 API 密钥是正确的,因此目前您需要应用服务器来发送这些类型的消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2012-08-04
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多