Java 极光推送分组推送

前提:
项目采用alias别名推送,极光推送按照别名推送,一次只能推1000条,因此,在数量>1000的情况下采用分组推送
jar包:极光官网提供的极光jar,除此之外还需要slf4j-api-1.7.5.jarslf4j-log4j12-1.7.5.jar

1.分组

    /**
     * @throws APIRequestException
     * @throws APIConnectionException
     * @throws Exception
     */
    private void pushMsgToUser(int type) throws APIConnectionException, APIRequestException, Exception {

        // 检索符合条件的别名
        List Devices = aliasService.selDevices(type);

        int size = Devices.size();
        List> pgrp = new ArrayList>();
        //超过999条分组推送,小与999条一次推送
        if (size > 998) {
            int n = 5;//假设分成5组
            int number=size/n;//每组的个数
            int remainder=size%n;//余数
            int offset=0;//偏移量

            for (int i=0 ; i < n ; i++ ){
                List pp = null;
                if (remainder > 0){
                    pp=Devices.subList(i*number+offset,(i+1)*number+offset+1);
                    remainder--;
                    offset++;
                }else {
                    pp=Devices.subList(i*number+offset, (i+1)*number+offset);
                }
                pgrp.add(pp);
            }
            for (List li : pgrp) {
                boolean result = JPushUtil.pushMsg(li, "有新信息,请注意查看。", "有新信息");
                if (!result) {
                    System.out.println("推送消息失败");
                }
            }
        }else {
            boolean result = JPushUtil.pushMsg(Devices, "有新信息,请注意查看。", "有新信息");
            if (!result) {
                System.out.println("推送消息失败");
            }
        }
    }

2.调用推送方法

public class JPushUtil {
    //自己项目的appkey和secret
    private static final String APPKEY ="";
    private static final String SECRET = "";
    //极光推送测试环境
    private static final boolean APNS_PRODUCTION = false;
    /**
     * 推送消息给用户
     * @param alias 别名
     * @param alert 标题
     * @param msgContent 内容
     * @param setApnsProduction
     * @return
     * @throws APIConnectionException
     * @throws APIRequestException
     */
    public static boolean pushMsg(List alias, String alert, String msgContent, String sound) throws APIConnectionException, APIRequestException {

        // 极光推送自定义参数变量
        Map extras = new HashMap();
        JPushClient jpushClient = new JPushClient(SECRET, APPKEY);
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .incrBadge(1)
                                .setSound("happy.caf")
                                .addExtra("from", "JPush")
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(alert)
                                .setTitle(msgContent)
                                .build()) 
                        .build())
                .setMessage(Message.content(msgContent))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(APNS_PRODUCTION)
                        .build())
                .build();
        PushResult pushResult = jpushClient.sendPush(payload);
        return pushResult.isResultOK();
    }
    }

你可能感兴趣的:(java)