Java 极光推送分组推送

来源:互联网 发布:郑州景安网络怎么样 编辑:程序博客网 时间:2024/06/05 16:42

前提:
项目采用alias别名推送,极光推送按照别名推送,一次只能推1000条,因此,在数量>1000的情况下采用分组推送

1.分组

    /**     * @throws APIRequestException     * @throws APIConnectionException     * @throws Exception     */    private void pushMsgToUser(String type) throws APIConnectionException, APIRequestException, Exception {        // 检索符合条件的别名        List<String> Devices = subjectService.selDevices();        int size = Devices.size();        List<List<String>> pgrp = new ArrayList<List<String>>();        //超过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<String> 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<String> li : pgrp) {                boolean result = KeCommon.pushMsgToTea(li, "有新信息,请注意查看。", "有新信息");                if (!result) {                    printErrorLog("批给推送消息失败", logger);                }            }        }else {            boolean result = KeCommon.pushMsgToTea(Devices, "有新信息,请注意查看。", "有新信息");            if (!result) {                printErrorLog("分批给老师推送消息失败", logger);            }        }    }

2.调用推送方法

public class KeCommon {    /**     * 推送消息给用户     * @param alias 别名     * @param alert 标题     * @param msgContent 内容     * @param setApnsProduction:true发布环境、false测试环境     * @return     * @throws APIConnectionException     * @throws APIRequestException     */    public static boolean pushMsgToTea(List<String> alias, String alert, String msgContent, String sound) throws APIConnectionException, APIRequestException {        // 极光推送自定义参数变量        Map<String, String> extras = new HashMap<String, String>();        JPushClient jpushClient = new JPushClient(MASTERSECRETTEA, APPKEYTEA);        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();        logger.debug("ios===:" + Platform.ios());        logger.debug("alias===:" + alias);        logger.debug("alert===:" + alert);        logger.debug("extras===:" + extras);        logger.debug("Notification===:" + Notification.ios(alert, extras));        logger.debug("Message===:" + msgContent);        PushResult pushResult = jpushClient.sendPush(payload);        return pushResult.isResultOK();    }    }