1

I've been following the Set Up a GCM Client App tutorial and tried to understand the demo app they provided, but I couldn't understand how to send push notification with this service.

The above guide leads me to a "Generating InstanceID token" screen with never-ending ProgressBar. This program source can be obtained here: https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main

After using the above source code I've tried next to Simple Downstream messaging, this: https://developers.google.com/cloud-messaging/downstream in order to send push to the app users.

I couldn't understand how I'm supposed to send my app users notifications with it.

I have a server and I'm great with PHP, What is left to do in order to send push notification to my application users using the GCM?

Finally managed to get the message to a TextView! How do make it into push?

public class GcmService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle data) {
        JSONObject jsonObject = new JSONObject();
        Set<String> keys = data.keySet();
        for (String key : keys) {
            try {
                jsonObject.put(key, data.get(key));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        try {
            sendNotification("Received: " + jsonObject.toString(5));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDeletedMessages() {
        sendNotification("Deleted messages on server");
    }

    @Override
    public void onMessageSent(String msgId) {
        sendNotification("Upstream message sent. Id=" + msgId);
    }

    @Override
    public void onSendError(String msgId, String error) {
        sendNotification("Upstream message send error. Id=" + msgId + ", error" + error);
    }

    private void sendNotification(final String msg) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (MainActivity.mTextView != null) {
                    MainActivity.mTextView.setText(msg);
                }
            }
        });
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
EliKo
  • 101
  • 7

1 Answers1

0

Let's assume you have created a new project at Google Developers Console and taken note of 2 values: Project Number, which will be used as SENDER_ID in a client project; and API server key (created at Credentials), which will be used as API_KEY in a server project.

You can find more about basic server project with my answers at the following questions. In the second link, you will find sample code for a server project in Java, then you can refer to its logic to implement in your PHP project:

Adding Google Cloud Messagin (GCM) for Android - Registration process

How to implement a GCM Hello World for Android using Android Studio

Hope this helps!

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87
  • Thank you! I've been following it and finally managed to get the message to my device! That's great. But it shows me the message on a textview I created, and I need it to show it as Push notification. How I can change this textview into push notification even when the user is not with the app active? – EliKo Oct 05 '15 at 08:02
  • For system notifications, IMO, you should go [here](http://developer.android.com/guide/topics/ui/notifiers/notifications.html), there's an example about `Creating a simple notification` – BNK Oct 05 '15 at 08:23
  • You post me 2 links, I'm following them and make GCM message show in a TextView, it works, and then you refer me to a different class I have no idea how to implement in the given code followed by the first 2 links. How I can implement this notification in the sendNotification method as edited above? – EliKo Oct 05 '15 at 09:53
  • Have you read the above link yet? Have you seen 'setContentText'? You can try put the snippet at Creating a simple notification section into sendNotification. I have not tested, you can test. – BNK Oct 05 '15 at 10:04