需求秒数:做一个给安装公司客户端的iphone用户推送广告的功能。
直接上代码。
使用jar包:JavaPNS_2.2.jar
/**
* push
* @author penghui.li
*
*/
public class ApplePushInfo {
private Log logger = LogFactory.getLog(this.getClass());
private PushDao pushDao;
//证书
private String keystore = ApplicationConfig.getApplicationConfig().getPropertie("push.iphone.keystore", "d:/apns.p12");
//证书密码
private String password = ApplicationConfig.getApplicationConfig().getPropertie("push.iphone.password", "123456");
private boolean production = ApplicationConfig.getApplicationConfig().getPropertie("push.iphone.production", "0").equals("0") ? true : false;
//线程数
private int threads = Integer.parseInt(ApplicationConfig.getApplicationConfig().getPropertie("push.iphone.threads.count", "30"));
public ApplePushInfo() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
pushDao = (PushDao) ctx.getBean("pushDao");
}
//程序入口
public static void main(String[] args) {
try {
new ApplePushInfo().push();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
public void push() {
long startTm = System.currentTimeMillis();
try {
//对apple反馈的数据进行处理
handleFeedback();
Map map = new HashMap();
//获得要发送的信息列表
List<PushInfoVo> list = pushDao.queryPushInfoList(map);
List<PushDeviceVo> ulist = new ArrayList<PushDeviceVo>();
for (PushInfoVo vo : list) {
map.put("type", vo.getType());
//获得要发送该信息的设备列表(即iphone的udid)
ulist = pushDao.queryPushUdidList(map);
List<Device> li = new ArrayList<Device>();
for (PushDeviceVo dVo : ulist) {
Device device = new BasicDevice();
device.setToken(dVo.getDeviceToken());
// device.setToken("e4becddba633695c9e911f2e8709ce53d3c7af557ace4efc9ae2675a00ae6ab7");
li.add(device);
}
startTm = System.currentTimeMillis();
if (li.size() > 0) {
send(li, vo.getInfo(), keystore, password, production);
}
logger.info("**********push:" + li.size() + "****************cost:[" + (System.currentTimeMillis() - startTm) + "]ms");
}
startTm = System.currentTimeMillis();
//发送成功后批量更新信息的状态
map.put("infoList", list);
if(list != null && list.size()>0){
pushDao.updatePushInfoStatus(list);
}else{
logger.info("*********infolist is null or this size is zero");
}
logger.info("**********update info:" + list.size() + "****************cost:[" + (System.currentTimeMillis() - startTm) + "]ms");
} catch (Exception ex) {
ex.printStackTrace();
logger.info(ex.toString());
}
}
/**
* 对apple反馈的数据进行处理
* @param devList
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void handleFeedback() {
long startTm = System.currentTimeMillis();
List<Device> feedBacklist = new ArrayList<Device>();
try {
feedBacklist = Push.feedback(keystore, password, production);
logger.info("**********getFeedback:" + feedBacklist.size() + "****************cost:[" + (System.currentTimeMillis() - startTm) + "]ms");
Map map = new HashMap();
map.put("devList", feedBacklist);
// /**********test start*************/
// Device device = new BasicDevice();
// device.setToken("test9fdb1188bdc27177065db15639af903d7f2b474041b02e4ddd303bcaca509");
// feedBacklist.add(device);
// /**********test end*************/
startTm = System.currentTimeMillis();
if(feedBacklist != null && feedBacklist.size()>0){
pushDao.updateDeviceStatus(feedBacklist);
}else{
logger.info("*********feedBacklist is null or this size is zero");
}
logger.info("**********update Device:" + feedBacklist.size() + "****************cost:[" + (System.currentTimeMillis() - startTm) + "]s");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.info(e.toString());
}
}
/**
* 发送
* @param devices
* @param message
* @param keystore
* @param password
* @param production
* @throws Exception
*/
@SuppressWarnings("unused")
public void send(List<Device> devices, String message, Object keystore,
String password, boolean production) throws Exception {
PushNotificationPayload payload = PushNotificationPayload.alert(message);
List<PushedNotification> notifications = Push.payload(payload,keystore, password, production, threads, devices);
int successNum = 0;
int failedNum = 0;
for (PushedNotification notification : notifications) {
if (notification.isSuccessful()) {
// System.out.println("Push notification sent successfully to: " +
// notification.getDevice().getToken());
successNum++;
} else {
failedNum++;
String invalidToken = notification.getDevice().getToken();
logger.info("**********Push notification sent failed to:token->[" + invalidToken+"]");
Exception theProblem = notification.getException();
// theProblem.printStackTrace();
ResponsePacket theErrorResponse = notification.getResponse();
if (theErrorResponse != null) {
// System.out.println(theErrorResponse.getMessage());
// logger.info("theErrorResponse.getMessage()");
}
}
}
logger.info("**********Push message:"+message+"**** total->["+devices.size()+"]***success->["+successNum+"]***failed["+failedNum+"]");
}
public PushDao getPushDao() {
return pushDao;
}
public void setPushDao(PushDao pushDao) {
this.pushDao = pushDao;
}
}
分享到:
相关推荐
Provider是指某个iPhone软件的Push服务器。 APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,下文统一使用该缩写。 因 此,整个过程可以分为三个阶段,下面用大家常用的聊天客户端BeejiveIM来...
iPhone iOS Push Test.
iPhone实现push功能的参考源码,pushMe作为服务器端,pushTest作为客户端。
Iphone开发系列源码——FaceBook基于Iphone的多个应用源码Iphone开发系列源码——FaceBook基于Iphone的多个应用源码Iphone开发系列源码——FaceBook基于Iphone的多个应用源码Iphone开发系列源码——FaceBook基于...
pki iphone 数字证书 digital certificate
iPhone手机应用软件模板是一款适合手机客户端开发企业网站模板。
iPhone应用程序编程指南-中文版 pdf iPhone应用程序编程指南-中文版 pdf iPhone应用程序编程指南-中文版 pdf
关于iphone中push notification得实现参考文档
ios获取cpu使用率,包括应用级别和手机级别。
炫彩iPhone6应用官网模板是一款好看的iPhone6手机APP应用网站模板。
炫彩iPhone6应用官网模板是一款好看的iPhone6手机APP应用网站模板。.rar
iPhone应用程序编程指南iPhone应用程序编程指南iPhone应用程序编程指南
这是一个完整的iphone推送机制的.net平台实现
iPhone手机信号测试强度,小区信号强度测试,基站信号测试
一个iphone聊天应用程序,服务端用的php搭建,实现两者之间的通信。
APP手机应用推广页模板是一款iPhone手机应用APP下载官网网站模板。
APP手机应用推广页模板是一款iPhone手机应用APP下载官网网站模板。.rar
iphone手机OA客户端 支持iphone 2代、2代、4代 支持ipad、itouch
iPhone应用程序编程指南(中文版)iPhone应用程序编程指南(中文版)iPhone应用程序编程指南(中文版)iPhone应用程序编程指南(中文版)iPhone应用程序编程指南(中文版)