极光开发服务 向终端设备推送消息

复制代码
 1 private static PushPayload buildPushObject_all_alias_alert(Integer mid, String message, Integer flag) {
 2             JSONObject result = new JSONObject();
 3             result.put("mid", mid);
 4             result.put("message", message);
 5             result.put("flag", flag);
 6             return PushPayload.newBuilder()
 7                     .setPlatform(Platform.all())
 8                     .setAudience(Audience.all())
 9                     .setNotification(Notification.alert(""))
10                     .setMessage(Message.content(result.toString()))
11                     .build();
12         }
复制代码
复制代码
//给机器发送信息
        public static void sendJPush(Integer mid, String message, Integer flag) {
            JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());

            // For push, all you need do is to build PushPayload object.
            PushPayload payload = buildPushObject_all_alias_alert(mid, message, flag);

            try {
                PushResult result = jpushClient.sendPush(payload);
                log.info("Got result - " + result);

            } catch (APIConnectionException e) {
                // Connection error, should retry later
                log.error("Connection error, should retry later", e);

            } catch (APIRequestException e) {
                // Should review the error, and fix the request
                log.error("Should review the error, and fix the request", e);
                log.info("HTTP Status: " + e.getStatus());
                log.info("Error Code: " + e.getErrorCode());
                log.info("Error Message: " + e.getErrorMessage());
            }
        }
复制代码
复制代码
 /**
     *测试消息机制
     */
    @RequestMapping(value = "/testJpush.json", method = RequestMethod.GET)
    @ResponseBody
    public void testJpush(Integer mid) {
        JSONObject json = new JSONObject();
        json.put("Untiekes", "0A-01,0B-01");
        json.put("Ono", "2019080114156688");
        //发送信息给android
        try {
            Utils.sendJPush(mid, "\"" + json + "\"", 4);
        } catch (Exception e) {
            log.info("推送消息异常=========================" + e.getMessage());
        }
    }
复制代码

 

  详情见极光开发官网-------https://docs.jiguang.cn//jpush/server/sdk/java_sdk/

 

posted @   tomingto  阅读(205)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
阅读排行:
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 如何打造一个高并发系统?
· 《SpringBoot》EasyExcel实现百万数据的导入导出

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示