Sending push notification using Google GCM

2020-04-20 14:32发布

问题:

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);
                }
            }
        });
    }
}

回答1:

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!