Android GCM java 服务器示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22261024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:43:34  来源:igfitidea点击:

GCM java server example

androidgoogle-cloud-messaging

提问by ViT-Vetal-

Does anyone have a link to GCM java 3rd Party Server example? I can't understand how to implement it. In http://developer.android.com/google/gcm/server.html, I could not find a complete example.

有没有人有 GCM java 3rd Party Server 示例的链接?我无法理解如何实现它。在http://developer.android.com/google/gcm/server.html 中,我找不到完整的示例。

回答by Eran

The easiest way is to use gcm-server.jar(which you can get from here).

最简单的方法是使用gcm-server.jar(您可以从这里获得)。

Then the code you'll need to send a GCM message will look like this :

然后您需要发送 GCM 消息的代码将如下所示:

Sender sender = new Sender(apiKey);
Message message = new Message.Builder()
    .addData("message", "this is the message")
    .addData("other-parameter", "some value")
    .build();
Result result = sender.send(message, registrationId, numOfRetries);

回答by Ash_P

I would like to share a more better approach which confirms message delivered successfully or not. We can do this using RestTemplateand HTTPPost.

我想分享一个更好的方法来确认消息是否成功传递。我们可以使用RestTemplateand来做到这一点HTTPPost

I will show how we can do it using gcm.jar

我将展示我们如何使用 gcm.jar

public class GCMJarGCM {
    public static final String GCM_API_KEY = "Get this API key from Google Developer Console";
    public static final String MESSAGE_VALUE = "Hello, Sending Notifications using GCM";    
    public static final String MESSAGE_KEY = "message";
    public static final String REG_ID = "This you'll get once you register on GCM";

    public static void main(String[] args) throws IOException {
        Sender sender = new Sender(GCM_API_KEY);

        ArrayList<String> devicesList = new ArrayList<String>();
        devicesList.add(REG_ID);

        Message message = new Message.Builder().timeToLive(30)
                .delayWhileIdle(true).addData(MESSAGE_KEY, MESSAGE_VALUE).build();

        MulticastResult result = sender.send(message, devicesList, 1);
        sender.send(message, devicesList, 1);
        System.out.println(result.toString());
    }
}

The output you'll see like below:

您将看到如下输出:

MulticastResult(multicast_id=4951949299732552396,total=1,success=1,failure=0,canonical_ids=0,results: [[ messageId=0:1445408305351488%2a748ce7f9fd7ecd ]]

If you want to use HTTPPost or RestTemplate then you need to use EndPoint: https://android.googleapis.com/gcm/send

如果你想使用 HTTPPost 或 RestTemplate 那么你需要使用 EndPoint:https://android.googleapis.com/gcm/send