前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java集成极光推送

Java集成极光推送

作者头像
Bobby
发布2019-04-09 18:39:19
2.1K0
发布2019-04-09 18:39:19
举报
文章被收录于专栏:开心码农开心码农

本文标题:Java集成极光推送

原始链接: https://shuibo.cn/jiguang-push.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。  首先是jar,这里因为我项目中用的是Maven,需要jar的可自行去官网下载jar  在Maven中的pom.xml文件中添加

代码语言:javascript
复制
<dependency>
	<groupid>cn.jpush.api</groupid>
	<artifactid>jpush-client</artifactid>
	<version>3.2.17</version>
</dependency>
<dependency>
	<groupid>cn.jpush.api</groupid>
	<artifactid>jiguang-common</artifactid>
	<version>1.0.3</version>
</dependency>

JPushExample .java

代码语言:javascript
复制
/**

极光推送
@author Bobby
*/
public class JPushExample {

/**
 * 全平台推送
 * @param parm
 */
public static void jpushAll(Map<string, string=""> parm) {
       //创建JPushClient
       JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);
       //创建option

       PushPayload payload = PushPayload.newBuilder()
            .setPlatform(Platform.all())  //所有平台的用户
            .setAudience(Audience.registrationId(parm.get("RegId")))//registrationId指定用户
            .setNotification(Notification.newBuilder()
                    .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(parm.get("msg"))
                            .setBadge(+1)
                            .setSound("happy")
                            .addExtras(parm)
                            .build())
                    .addPlatformNotification(AndroidNotification.newBuilder()
                            .addExtras(parm)
                            .setAlert(parm.get("msg"))
                            .build())
                    .build())
            .setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境
            .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
            .build();
       try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }    
   }
   /**
    * android推送
    * @param parm
    */
   public static void jpushAndroid(Map<string, string=""> parm) {
       //创建JPushClient
       JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);
       //推送的关键,构造一个payload 
       PushPayload payload = PushPayload.newBuilder()
            .setPlatform(Platform.android())//指定android平台的用户
            .setAudience(Audience.all())//你项目中的所有用户
            .setNotification(Notification.android(parm.get("msg"), "XXX", parm))
            .setOptions(Options.newBuilder().setApnsProduction(false).build())
            //这里是指定开发环境,不用设置也没关系
            .setMessage(Message.content(parm.get("msg")))
            .build();

       try {
            PushResult pu = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }    
   }
   
   /**
    * 推送IOS
    * @param parm
    */
   public static  void jpushIOS(Map<string, string=""> parm) {
       //创建JPushClient
       JPushClient jpushClient = new JPushClient(JPushConfig.MasterSecret, JPushConfig.AppKey);
       PushPayload payload = PushPayload.newBuilder()
            .setPlatform(Platform.ios())//ios平台的用户
            .setAudience(Audience.all())//所有用户
            .setNotification(Notification.newBuilder()
            .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(parm.get("msg"))
                            .setBadge(+1)
                            .setSound("happy")
                            .addExtras(parm)
                            .build())
                    .build())
            .setOptions(Options.newBuilder().setApnsProduction(false).build())
            .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
            .build();

       try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }    
   }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年03月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档