(服务端)极光推送根据别名和分组名进行推送

1、别名推送
public static void sendPush(List list, Map map,String taskName) {
        JPushClient jpushClient = new JPushClient(PushTool.masterSecret, PushTool.appKey, 3);
        PushPayload payload = null;
        if (list != null && list.size() > 0) {
            payload = buildPushObject_android_and_iosByAlias(list, map, taskName);
        } else {
            payload = buildPushPushPayloadToAll(map);
        }
        try {
            PushResult result = jpushClient.sendPush(payload);
            LOG.info("Got result - " + result);
            
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
            
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
            LOG.info("Msg ID: " + e.getMsgId());
        }
	}
 /**
     * 根据别名指定唯一发送者
     * @param alias
     * @param map
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public static PushPayload buildPushObject_android_and_iosByAlias(List alias,Map map, String taskName) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                .setAlert(taskName)
                .addPlatformNotification(AndroidNotification.newBuilder()
                .setTitle(TITLE).addExtra("content", JSON.toJSONString(map)).build())
                .addPlatformNotification(IosNotification.newBuilder()
                .incrBadge(1)
                .addExtra("content", JSON.toJSONString(map)).build())
                .build())
                .build();
    }


//组装数据
public static void main(String[] args) {
    		ArrayList list = new ArrayList(0);
    		list.add("13");//list存放别名值,可以存放多个值
    		Map map = new HashMap(0);
    		map.put("id", "3542");
		sendPush(list, map,"1223");
	}

2、分组推送
public static void main(String[] args) {
   testSendPush();
}

public static void testSendPush() {
	    // HttpProxy proxy = new HttpProxy("localhost", 3128);
	    // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
		ClientConfig clientConfig = ClientConfig.getInstance();
        final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
        String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
        // Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
        NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig);
        // Call setHttpClient to set httpClient,
        // If you don't invoke this method, default httpClient will use NativeHttpClient.
//        ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
        jpushClient.getPushClient().setHttpClient(httpClient);
        final PushPayload payload = buildPushObject_android_and_ios();
//        for(int i=0;i<10;i++) {
//            Thread thread = new Thread() {
//                public void run() {
//                    for (int j = 0; j < 200; j++) {
//                        long start = System.currentTimeMillis();
                        try {
                            PushResult result = jpushClient.sendPush(payload);
                            LOG.info("Got result - " + result);

                        } catch (APIConnectionException e) {
                            LOG.error("Connection error. Should retry later. ", e);
                            LOG.error("Sendno: " + payload.getSendno());

                        } catch (APIRequestException e) {
                            LOG.error("Error response from JPush server. Should review and fix it. ", e);
                            LOG.info("HTTP Status: " + e.getStatus());
                            LOG.info("Error Code: " + e.getErrorCode());
                            LOG.info("Error Message: " + e.getErrorMessage());
                            LOG.info("Msg ID: " + e.getMsgId());
                            LOG.error("Sendno: " + payload.getSendno());
                        }

//                        System.out.println("耗时" + (System.currentTimeMillis() - start) + "毫秒 sendCount:" + (++sendCount));
//                    }
//                }
//            };
//            thread.start();
//        }

//        // For push, all you need do is to build PushPayload object.
//        PushPayload payload = buildPushObject_all_alias_alert();
//        try {
//            PushResult result = jpushClient.sendPush(payload);
//            LOG.info("Got result - " + result);
//            // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
//            // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
//            // jpushClient.close();
//        } catch (APIConnectionException e) {
//            LOG.error("Connection error. Should retry later. ", e);
//            LOG.error("Sendno: " + payload.getSendno());
//
//        } catch (APIRequestException e) {
//            LOG.error("Error response from JPush server. Should review and fix it. ", e);
//            LOG.info("HTTP Status: " + e.getStatus());
//            LOG.info("Error Code: " + e.getErrorCode());
//            LOG.info("Error Message: " + e.getErrorMessage());
//            LOG.info("Msg ID: " + e.getMsgId());
//            LOG.error("Sendno: " + payload.getSendno());
//        }
    }
 public static PushPayload buildPushObject_android_and_ios() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag("111"))//设置别名
                .setNotification(Notification.newBuilder()
                		.setAlert("alert content")
                		.addPlatformNotification(AndroidNotification.newBuilder()
                				.setTitle("Android Title").build())
                		.addPlatformNotification(IosNotification.newBuilder()
                				.incrBadge(1)
                				.addExtra("extra_key", "extra_value").build())
                		.build())
                .build();
    }

pom文件
		
			org.hibernate.java-persistence
			jpa-api
		
		
			org.hibernate
			hibernate-entitymanager
		

		
			org.hibernate
			hibernate-validator
		

		
			org.springframework
			spring-oxm
		
		
			org.springframework
			spring-test
		
		
			org.springframework
			spring-web
		
		
			org.springframework
			spring-webmvc
		
		
			org.springframework
			spring-webmvc-portlet
		
		
			org.springframework
			spring-tx
		
		
			org.springframework
			spring-core
		
		
			org.springframework
			spring-aop
		
		
			org.springframework
			spring-beans
		
		
			org.springframework
			spring-aspects
		
		
			org.springframework
			spring-beans
		
		
			org.springframework
			spring-context
		
		
			org.springframework
			spring-context-support
		
		
			org.springframework
			spring-expression
		
		
			org.springframework
			spring-jdbc
		
		
			org.springframework
			spring-orm
		
		
			org.springframework
			spring-jms
		
		
			org.codehaus.jackson
			jackson-core-asl
		
		
			org.codehaus.jackson
			jackson-mapper-asl
		
		
			javax.servlet
			jstl
		
		
			commons-lang
			commons-lang
		
		
			mysql
			mysql-connector-java
		
		
			opensymphony
			sitemesh
		
		
			aspectj
			aspectjweaver
		
		
			asm
			asm
		
		
			org.slf4j
			slf4j-api
		
		
			ch.qos.logback
			logback-classic
		

		
			junit
			junit
		
		
			javax.servlet
			servlet-api
		
		
			commons-fileupload
			commons-fileupload
		
		
			commons-io
			commons-io
		
		
			commons-logging
			commons-logging
		
		
			opensymphony
			quartz
		
		
			com.alibaba
			fastjson
		
		
			 spy
		     spymemcached
		
		
		    cn.jpush.api
		    jpush-client
		    3.2.9
		







你可能感兴趣的:(其它)