class="java">package com.malone.common; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.Lists; import com.malone.exception.OperateFailureException; import javapns.devices.Device; import javapns.devices.implementations.basic.BasicDevice; import javapns.notification.AppleNotificationServerBasicImpl; import javapns.notification.PushNotificationManager; import javapns.notification.PushNotificationPayload; import javapns.notification.PushedNotification; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; /** * Created with IntelliJ IDEA. * User: malone * Date: 14-7-19 * Time: 上午10:25 * To change this template use File | Settings | File Templates. */ public class PushUtils { private static Logger logger = LoggerFactory.getLogger(PushUtils.class); public static void main (String[] args) { try { push("2cd260c1876ca094a9452af0dd3595aaead748be0925314050b94ca5061d4f73", "这是测试推送消息"); } catch (Exception e) { e.printStackTrace(); } } /** * 一次给多个手机推送消息 * @param tokens */ public static void push (String tokens, String msg) throws Exception { //发送失败token记录 List<String> failureToken = Lists.newArrayList(); //截取 List<String> tokenList = Splitter.on(",").omitEmptyStrings().splitToList(tokens); if (tokenList == null || tokenList.size() == 0) { throw new OperateFailureException("请提供要发送的token!"); } if (StringUtils.isBlank(msg)) { throw new OperateFailureException("请提供要发送的消息!"); } //包装发送消息相关参数 PushNotificationPayload payLoad = new PushNotificationPayload(); payLoad.addAlert(msg); // 消息内容 payLoad.addBadge(PushConsts.badge); // iphone应用图标上小红圈上的数值 if (!StringUtils.isBlank(PushConsts.pushMsgSound)) { payLoad.addSound(PushConsts.pushMsgSound);//铃音 } //构造推送服务 PushNotificationManager pushManager = new PushNotificationManager(); //true:表示的是产品发布推送服务 false:表示的是产品测试推送服务 pushManager.initializeConnection(new AppleNotificationServerBasicImpl(PushConsts.certificateLocalPath, PushConsts.certificatePwd, PushConsts.msgFlag)); List<PushedNotification> notifications = new ArrayList<PushedNotification>(); //当单个发送失败,不应该影响其他的发送 for (String token : tokenList) { try { PushedNotification notification = pushManager.sendNotification(new BasicDevice(token), payLoad, true); notifications.add(notification); } catch (Exception e) { failureToken.add(token); logger.error("===============================一次失败开始=================================="); logger.error("token:" + token + ",msg:" + msg + "推送消息失败!"); logger.error(e.getMessage(), e); e.printStackTrace(); logger.error("===============================一次失败结束=================================="); } } //发送消息 // notifications = pushManager.sendNotifications(payLoad, device); List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications); List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications); int failed = failedNotifications.size(); int successful = successfulNotifications.size(); pushManager.stopConnection(); //是否失败 if (failureToken.size() > 0) { throw new OperateFailureException("如下token推送消息失败:" + Joiner.on(",").join(failureToken.toArray())); } if (failed >= 1) { throw new OperateFailureException("有" + failed + "个消息推送失败!"); } } /** * 推送用到的常量 */ public static interface PushConsts { /** * 推送消息的默认响铃 */ public String pushMsgSound = "default"; /** * 图标小红圈的数值 */ public int badge = 100; /** * 推送消息证书的本地路径 */ public String certificateLocalPath = "F:/ios push/sf push message.p12"; /** * 推送证书的密码 */ public String certificatePwd = "sf@push"; /** * 正式环境推送/开发环境推送 */ public boolean msgFlag = false; } }
?
?