• 极光推送-服务端代码


    1.依赖

    1.1.maven

    pom.xml

      <dependency>
          <groupId>cn.jpush.api</groupId>
          <artifactId>jpush-client</artifactId>
          <version>3.3.3</version>
      </dependency>
    <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.0.8</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.6.Final</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <!-- For log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>

    1.2.jar包

    jpush-client-3.3.3.jar

    jiguang-common-1.0.8.jar

    netty-all-4.1.6.Final.jar

    gson-2.3.jar

    slf4j-api-1.7.7.jar

    2.推送工具类

    2.1.代码示例

        /**
         * 调用极光推送,推送消息
         * @param msg 消息内容
         * @param title 消息标题
         * @param alias 设备码
         * @param extras 额外信息(用于前台传递数据)
         * @return
         */
        public static boolean send(String msg, String title,String alias,Map<String ,String> extras) {
            //极光推送企业账号信息
            String MASTER_SECRET = getProperty("MASTER_SECRET");
            String APP_KEY =getProperty("APP_KEY");
            
            //加密设备码
            String aliasMd5 = MD5Util.md5Digest(alias).toLowerCase();
            logger.error("=====MASTER_SECRET="+MASTER_SECRET);
            logger.error("=====APP_KEY="+APP_KEY);
            logger.error("=====alias="+alias);
            logger.error("=====aliasMd5="+aliasMd5);
            logger.error("=====extras="+extras.get("userType").toString());
            
            //获取设备类型
            String equipmentType = extras.get("equipmentType");    
            
            //获取 payload 对象
            PushPayload payload = null;
            if( equipmentType != null && StringUtil2.equals("ios", equipmentType)){
                payload = buildPushObject_ios_alias_alertWithTitle(msg, title,aliasMd5,extras);
            }else if(equipmentType != null && StringUtil2.equals("android", equipmentType)){            
                payload = buildPushObject_android_alias_alertWithTitle(msg, title,aliasMd5,extras);
            }else{
                logger.info("equipmentType 为空");
                return false;
            }
            //获取 jpushClient 对象
            JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null,ClientConfig.getInstance());
            try {
                PushResult result = jpushClient.sendPush(payload);
                logger.info(LOGO_TXT + result);
                return result.isResultOK();
            } catch (APIConnectionException e) {
                e.printStackTrace();
                // Connection error, should retry later
                logger.error(LOGO_TXT+"Connection error, should retry later", e);
    
            } catch (APIRequestException eapi) {
                eapi.printStackTrace();
                // Should review the error, and fix the request
                logger.error(LOGO_TXT+"Should review the error, and fix the request", eapi);
                logger.info(LOGO_TXT+"HTTP Status: " + eapi.getStatus());
                logger.info(LOGO_TXT+"Error Code: " + eapi.getErrorCode());
                logger.info(LOGO_TXT+"Error Message: " + eapi.getErrorMessage());
            }
            return false;
        }
        public static PushPayload buildPushObject_android_alias_alertWithTitle(
                String msg, String title, String alias ,Map<String,String> extras) {
            
            return PushPayload.newBuilder().setPlatform(Platform.android())
                    .setAudience(Audience.alias(alias))
                    .setNotification(Notification.android(msg, title, extras))
                    .build();
        }
        
        public static PushPayload buildPushObject_ios_alias_alertWithTitle(
                String msg, String title, String alias ,Map<String,String> extras) {
    
            return PushPayload.newBuilder().setPlatform(Platform.ios())
                    .setAudience(Audience.alias(alias))
                    .setNotification(Notification.ios("【"+title+"】"+msg, extras)).
                    setOptions(Options.newBuilder().setApnsProduction(true).build())
                    .build();
        }

    3.测试方法

    3.1.代码测试

        public static void main(String[] args) {
            String msg ="msgtest111";
            String title ="title11";
            String alias ="333333";
    //        String alias ="FB7E44E6-CBFC-49EB-B18A-3DC51D609DC9";
            String url = "www.baidu.com";
            Map<String ,String> m = new HashMap<String, String>();
            m.put("equipmentType", "ios");
            m.put("userType", "custromer");
            PushUtil.send(msg, title, alias,m);
        }

    3.2.网站验证

     登录极光推送网站,登录企业账户,查询推送历史

    https://docs.jiguang.cn/jpush/

  • 相关阅读:
    序列&权限&索引&视图的语句
    currval &nextval的差异理解
    数据库表的创建、修改、清空、数据的插入、更新、删除 语句
    数据库(日期、字符大小写控制、转换)函数
    数据库的相关语句(where,order by)
    多线程的两种方法(卖票系统展示)
    杨辉三角的几种方法
    网络编程的基础代码
    文件的复制(逐字节/整体)
    gui小计算器的程序写法
  • 原文地址:https://www.cnblogs.com/MIC2016/p/8193821.html
Copyright © 2020-2023  润新知