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; } }
相关推荐
IOS推送Java实现
苹果消息推送 java端,可以运行,包括所需的JAR包
ios消息推送java服务端实现 java push notification
完整版Java写IOS后台推送,添加证书,换下DeviceToken就可以实现苹果后台推送链接
苹果ios推送java服务端耗码demo
使用java语言开发ISO系统推送功能,包括所有依赖jar包、详细代码、开发步骤等
Java APNS推送消息 HTTP/2协议, 基于JDK 11及以上
APNS(Apple Push Notification Service)苹果推送通知服务。该技术由苹果公司提供的APNS服务。工作原理:首先,APNS会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书已确定其为...
IOS推送JAVA服务端文档,javaPNS2.2
极光推送使用java后台接入demo,推送会通知到Android和Ios的手机通知栏
ios 消息推送 java后端demo(包括测试代码所需要的jar包)
java实现IOS推送 运用javapns 包含包和基本的数据库配置
这个压缩包里面包含三个DOC文件:1.iphone推送java实现.doc 2.iphone推送简单JAVA示例.doc 3.实现iphone推送服务端原理.doc 三个文档很全面的指导学者学习怎么用java实现ios推送功能,不仅仅是讲解怎么实现,还讲解...
阿里云EAMS消息推送Java异步接入,给Android和IOS推送消息
基于Java的 iOS 推送通知 1.iOS 推荐原理 2.Java & iOS code
java服务端推送消息给ios客户端,有兴趣的朋友可以看下
总结iOS 的推送
IOS 消息推送 java服务端和客户端源码
java实现苹果推送 里面有两个方法实现推送
ios推送需要的包,java后台