且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发送推送通知GCM在Java

更新时间:2023-02-16 21:10:17

您可以使用gcm-server.jar其中包含辅助方法GCM消息。 为了得到这个罐子可以安装的[德precated]谷歌云消息传递为Android库通过Android SDK中管理器。不要让德precated名称迷惑你。只有客户端部分去precated,而不是服务器端。
安装时,可以在找到它之后ADT_SDKROOT \ SDK \演员\谷歌\ GCM。示例文件夹包含一个演示服务器,这是非常容易理解的。照片 发送GCM信息只涉及少数code线:

You can use gcm-server.jar which 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:

    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;