码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS】push通知(JavaEE实现)

时间:2014-12-27 20:18:07      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

第一次做iOS push,回想起来还挺简单的。
因为做的时候没留下图,所以就不上图了

去http://developer.apple.com ,用付过费的开发者帐号,创建一个新的应用。勾选上Enable Push Notification;
然后在本机的Key Chain 中请求从证书颁发机构请求一个证书。 上传, 下载这一套流程我就不说,网上很多

主要介绍一下psuh的详细过程

证书什么的, 客户端,我都已经配置好了。
可服务端的人没做过这个。没办法,我来封装一下服务端的代码吧。

我选择了Java Apns

先上一段我封装好的JavaEE的代码

public interface APNSFeedBack {
public void APNSFeedBack(Boolean success,Exception exception);
}

 

public class ApplePush extends Thread {

private PushNotificationManager pushNotificationManager;
private AppleNotificationServer appleNotificationServer;
private ArrayList<Device> devices;
private Payload payload;
private APNSFeedBack feedBack;

static public void main(String argc[])
{

try {

String tokenDevice = "0b307cfe5d55a3018f281d68afb7ae188c102628b441587e526707fd41e62125";

PushNotificationPayload payload = new PushNotificationPayload();

payload.addAlert("大家好啊");

ArrayList<String> devicesString = new ArrayList<String>();

devicesString.add(tokenDevice);

ApplePush push = new ApplePush();

push.sendNotification(devicesString, payload, new APNSFeedBack() {
@Override
public void APNSFeedBack(Boolean success, Exception exception) {
if (success) {
System.out.println("请求成功");
} else {
System.out.println("请求失败 exception = " + exception);
}
}
});

System.out.println("正在push中…");
}catch (Exception exception)
{
System.out.println("Json库加载失败");
}

}

public Boolean sendNotification(ArrayList<String>devicesString,PushNotificationPayload payload,APNSFeedBack feedBack)
{

//初始化
try
{

String p12Path = "/Users/WangHeShiDai/Desktop/PushKey.p12"; //证书路径
String password = "wangheshidai"; //密码
Boolean isDeveloper = true; //是不是开发模式

if (devicesString == null || devicesString.size() == 0)
{
feedBack.APNSFeedBack(false,new Exception("设备惟一标识异常"));
}
//构建Device

this.devices = new ArrayList<Device>(devicesString.size());

for (int i = 0 ; i < devicesString.size() ; i ++)
{
String deviceString = devicesString.get(i);

Device device = new BasicDevice();

device.setToken(deviceString);

this.devices.add(device);
}

//保存句柄
this.payload = payload;
this.feedBack = feedBack;

//push管理程序
this.pushNotificationManager = new PushNotificationManager();

//初始化证书和端口
this.appleNotificationServer = new AppleNotificationServerBasicImpl(p12Path,password,!isDeveloper);

this.start();

}
catch (Exception ex)
{
System.out.print("抛出异常");
ex.printStackTrace();
feedBack.APNSFeedBack(false,ex);
}

return true;
}

@Override
public void run() {

try {

//连接苹果服务器
this.pushNotificationManager.initializeConnection(this.appleNotificationServer);
//发送消息
this.pushNotificationManager.sendNotifications(this.payload, this.devices);
//断开链接
this.pushNotificationManager.stopConnection();
System.out.println("发送成功");
this.feedBack.APNSFeedBack(true,null);
} catch (Exception exception)
{
System.out.print("抛出异常");
exception.printStackTrace();
this.feedBack.APNSFeedBack(false,exception);
}

}
}

 

【iOS】push通知(JavaEE实现)

标签:

原文地址:http://www.cnblogs.com/onlywish/p/4189025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!