android GCM doesn't work

2019-01-15 09:11发布

问题:

I use the example provided by goole and I built an client side application to get notifications and a server side, both applications (code) is those provided by google.

I run the application on the client side and I get the registration_id. some huge string, in this format which is below:

APA91bEgguwt98xLbivrXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXLxDalNoEktCSVbOkT0-uFH2FaRnfpkRME2tzuvr0BycMNEhX_Ix1PV_XXXXXXXXXXXXXXXXXXXXXN5hcseY_wA.

This proves me that the client application works. Now the server (java, google example):

public static void main(String[] args) {

        Sender sender = new Sender("AIzaSXXXXXXXXXX_XXXXXXXXXXXXXXXrQOnoGZw");      
        Message message = new Message.Builder()
        .collapseKey("1")
        .timeToLive(3)
        .delayWhileIdle(true)
        .addData("message",
          "this text will be seen in notification bar!!")
        .build();

        try {
            Result result = sender.send(message, "APA91bEgguwt98xLbivrXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXLxDalNoEktCSVbOkT0-uFH2FaRnfpkRME2tzuvr0BycMNEhX_Ix1PV_XXXXXXXXXXXXXXXXXXXXXN5hcseY_wA", 3);
            System.out.println(result.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

This is the server. When I run the server I got as result something like this: [ messageId=0:1341907115903155%921c249a00000031 ] but no message on the client and I don't understand why. What is the meaning of the response I got from server? Is it the success response?

I need help to understand what is wrong?

回答1:

According to the GCM Documentation:

When a 3rd-party server posts a message to GCM and receives a message ID back, it does not mean that the message was already delivered to the device. Rather, it means that it was accepted for delivery. What happens to the message after it is accepted depends on many factors.

In the best-case scenario, if the device is connected to GCM, the screen is on, and there are no throttling restrictions (see Throttling), the message will be delivered right away.



回答2:

Please review their comment

http://developer.android.com/guide/google/gcm/gcm.html

Note: If your organization has a firewall that restricts the traffic to or from the Internet, you'll need to configure it to allow connectivity with GCM. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn't provide specific IPs. It changes IPs frequently.



回答3:

You can follow this link to understand the GCM.The following link worked for me: http://android.amolgupta.in/2012/07/google-cloud-messaging-gcm-tutorial.html



回答4:

Remove your internet firewall if you are using it and try again

Please go through this



回答5:

Please take a look at the following link also

http://androidv5.wordpress.com/2012/08/15/how-to-implement-google-cloud-messaging/



回答6:

If your facing delay notification or message problem in GCM then try this solution.

I know this is not proper solution but it's WORKS Install this app it's really work for me HERE