极光推送—java快速接入

来源:互联网 发布:淘宝首页设计价格 编辑:程序博客网 时间:2024/05/18 03:53

此文记录自己开发项目时,java快速接入极光推送的过程,免去研究官方文档的流程!
详情也可参考 [极光官网] (http://docs.jiguang.cn/jpush/guideline/intro/)

新手问题解惑:

问题:初次接触极光推送,分不清推送的对象?答:

  • 广播:会把通知无区别的推送到每个人身上。
  • 设置标签:这一般用于群组推送。
  • 设置别名:适用于单播,根据客户端设置的别名来推送。(一般选择用户ID)
  • 设置注册ID:适用于单播推送,指定推送给某一个人,可以使用注册过的用户ID,主要用来区分。

别名,注册ID,标签,都需要跟客户端开发人员沟通确定统一的规则,一般会使用userId作为单播标识,


接入步骤如下:

步骤1:

在java项目中创建工具类class,例:JpushClient.java

步骤2:

在maven pom.xml文件中添加依赖关系

 <dependency>      <groupId>cn.jpush.api</groupId>      <artifactId>jpush-client</artifactId>      <version>3.2.17</version>    </dependency>
步骤3:

在JpushClient.java类中,推送方法,参考如下两段代码

基本配置代码
 //在极光注册的APPKEY和MASTERSECRET    必填    private static final String APPKEY ="ad7ba99aa8ad92a4c5";    private static final String MASTERSECRET ="51feea7371bef4";    private static JPushClient jpushClient = null;    //保存离线的时长,最多支持10天     (Ps:不填写时,默认是保存一天的离线消息     0:代表不保存离线消息)    private static int timeToLive = 60 * 60 * 24 ;    private static Logger logger = LoggerFactory.getLogger(JpushClient.class);
构建推送对象:别名推送 例:关注推送

type:是自己设定的值,前后端保持一致即可
请求参数:是推送的自己要输入的内容

 public static PushPayload buildPushObject_alias_followUser(String alias ,String nickname) {        return PushPayload.newBuilder().setPlatform(Platform.android_ios())                .setAudience(Audience.alias(alias))                .setNotification(Notification.newBuilder()                        .addPlatformNotification(AndroidNotification.newBuilder()                                .addExtra("type", "infomation")                                .setAlert(nickname+" 关注了你!")                                .build())                        .addPlatformNotification(IosNotification.newBuilder()                                .addExtra("type", "infomation")                                .setAlert(nickname+" 关注了你!")                                .build())                        .build())                .setOptions(Options.newBuilder()                        .setApnsProduction(true)//true-推送生产环境 false-推送开发环境(测试使用参数)                        .setTimeToLive(timeToLive)                        .build())                .build();    }
发送推送消息 send message

此方法调用了,上面的构建对象方法,将构建好的对象推送给手机端

  //send message after followUser    public static void sendPushAfterFollow(String alias ,String nickname){        try {            jpushClient = new JPushClient(MASTERSECRET, APPKEY, null, ClientConfig.getInstance());            //生成推送的内容            PushPayload payload = buildPushObject_alias_followUser(alias,nickname);            payload.resetOptionsTimeToLive(timeToLive);            PushResult result = jpushClient.sendPush(payload);            logger.info("Got result - " + result);        } catch (APIConnectionException e) {            // Connection error, should retry later            logger.error("Connection error, should retry later", e);        } catch (APIRequestException e) {            // Should review the error, and fix the request            logger.error("Should review the error, and fix the request", e);            logger.info("HTTP Status: " + e.getStatus());            logger.info("Error Code: " + e.getErrorCode());            logger.info("Error Message: " + e.getErrorMessage());        }    }

步骤4:在项目业务层service层调用,上面的工具类中的方法即可进行推送
   //向指定用户推送消息        JpushClient.sendPushAfterFavorite(参数1,参数2...);

至此,java快速接入极光推送已经完成,过程非常简单,详细的参数规则,请参考极光官网!


作者:Moli_wu
链接:http://www.jianshu.com/p/22a4d630c01a
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。