zoukankan      html  css  js  c++  java
  • 极光推送-服务端代码

    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/

  • 相关阅读:
    大数据之路week07--day05 (一个基于Hadoop的数据仓库建模工具之一 HIve)
    大数据之路week07--day04 (Linux 中查看文件内容的关键字处)
    大数据之路week07--day04 (YARN,Hadoop的优化,combline,join思想,)
    hdu 1575 Tr A(矩阵快速幂,简单)
    hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
    zoj 2974 Just Pour the Water (矩阵快速幂,简单)
    LightOj 1065
    LightOj 1096
    poj 1006 生理周期(中国剩余定理)
    POJ 2251 Dungeon Master(广搜,三维,简单)
  • 原文地址:https://www.cnblogs.com/MIC2016/p/8193821.html
Copyright © 2011-2022 走看看