0

I followed Google Cloud Messaging (GCM) with local device groups on Android gives HTTP Error code 401 to manage local device groups on Android and successfully got a notification key, but when I send message to the notification key, I never get the message back. Has anyone ever got this work?

My send code is like:

    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();
}
Community
  • 1
  • 1
Andy Chen
  • 1
  • 1

2 Answers2

0

It seems there's a misunderstanding about the GCM concept. The app server is an integral part of GCM messaging.

The server side of Google Cloud Messaging (GCM) consists of two components:

  • GCM connection servers provided by Google. These servers take messages from an app server and send them to a client app running on a device. Google provides connection servers for HTTP and XMPP.
  • An application server that you must implement in your environment. This application server sends data to a client app via the chosen GCM connection server, using the appropriate XMPP or HTTP protocol.

Try the Android GCM Playground to get a better understanding of this.

Here's a snippet:

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");
            }
        }
    }
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • 1
    I'm trying to avoid app server, and avoid embedding api key in the app source code. I'm sure it is possible send/receive messages without an app server but API key has to be in the source code. That's why I'm using local device groups. Thanks. – Andy Chen Jun 07 '16 at 13:35
0

The to field of the send method represents the sender ID of your project. You cannot use this method to send messages to Instance ID tokens (other devices), Device to Device messaging is not currently supported by GCM.

You are correct to avoid including the API key in your client app, so currently you will need an app server to send these types of messages.

Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33