通过java发送推送通知GCM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21853129/
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-13 11:03:42  来源:igfitidea点击:

Send push notification GCM by java

javaandroidpush-notificationgoogle-cloud-messaging

提问by user3302527

I have configured a client android Google Cloud Messaging (GCM) to receive push notifications, but I can not configure a server in java to send notifications to devices. How could I?

我已经配置了一个客户端 android Google Cloud Messaging (GCM) 来接收推送通知,但我无法在 java 中配置服务器来向设备发送通知。我怎么能?

采纳答案by sinujohn

You can use gcm-server.jarwhich contains helper methods for GCM messaging. To get this jar you can install "[Deprecated]Google Cloud Messaging for Android Library"through Android SDK Manager. Don't let the deprecated name confuse you. Only the client part is deprecated, not server side.
After install you can find it at "ADT_SDKROOT\sdk\extras\google\gcm". The sample folder contains a demo server which is very easy to understand.
Sending a GCM message involves only few lines of code:

您可以使用gcm-server.jar,其中包含用于 GCM 消息传递的辅助方法。要获取此 jar,您可以通过 Android SDK 管理器安装“[Deprecated]Google Cloud Messaging for Android Library”。不要让弃用的名称使您感到困惑。只有客户端部分被弃用,而不是服务器端。
安装后,您可以在"ADT_SDKROOT\sdk\extras\google\gcm". 示例文件夹包含一个非常容易理解的演示服务器。
发送 GCM 消息只涉及几行代码:

    final String GCM_API_KEY = "yourKey";
    final int retries = 3;
    final String notificationToken = "deviceNotificationToken";
    Sender sender = new Sender(GCM_API_KEY);
    Message msg = new Message.Builder().build();

    try {
                Result result = sender.send(msg, notificationToken, retries);

                if (StringUtils.isEmpty(result.getErrorCodeName())) {
                    logger.debug("GCM Notification is sent successfully");
                    return true;
                }

                logger.error("Error occurred while sending push notification :" + result.getErrorCodeName());
    } catch (InvalidRequestException e) {
                logger.error("Invalid Request", e);
    } catch (IOException e) {
                logger.error("IO Exception", e);
    }
    return false;

回答by nurisezgin

For """test""" create java console app, add gcm jar file.

对于 """test""" 创建 java 控制台应用程序,添加 gcm jar 文件。

    try{
     Sender sender = new Sender("<senderId>");
     ArrayList<String> devicesList = new ArrayList<String>();
     devicesList.add(<deviceId>);
     String data = "<data>";
     Message message = new Message.Builder()
                        .collapseKey("1")
                        .timeToLive(3)
                        .delayWhileIdle(true)
                        .addData("message",
                                data)
                        .build();
    MulticastResult result = sender.send(message, devicesList, 1);
                sender.send(message, devicesList, 1);

                System.out.println(result.toString());
                if (result.getResults() != null) {
                    int canonicalRegId = result.getCanonicalIds();
                    if (canonicalRegId != 0) {
                    }
                } else {
                    int error = result.getFailure();
                    System.out.println(error);
                }

}