消息推送

public void doOpen() throws Exception {
certificatePath = "zn_server.p12";
production = false;
certificatePassword = "123456";
if(ansBasicImpl == null && StringUtils.isNotBlank(certificatePath)){
try {
String fullPath = PushMessageServiceImpl.class.getClassLoader().getResource(certificatePath).getPath();
ansBasicImpl = new AppleNotificationServerBasicImpl(fullPath, certificatePassword, production);

// 与apple建立连接
pushManager.initializeConnection(ansBasicImpl);

logger.info("与apple建立连接1成功");

} catch (Exception e) {
e.printStackTrace();
logger.info("Ios doInitializeConnection: txnId= Ios execute :" + e.getMessage());
           logger.info("=AppleApnsSAO:与apple建立连接1异常! "+e);
           
ansBasicImpl = null;

}

}

public void push2IOS(String token, String message, Map<String, String> customParam){
try {
// 普通定制参数
PushNotificationPayload payload = new PushNotificationPayload();
payload.addAlert(message); //提醒文本
payload.addSound("default"); //提醒铃声
payload.addBadge(1);

this.addCustomDictionary(payload, customParam);

Device device = new BasicDevice(token);

// 发送信息到apple服务器
PushedNotification notification = pushManager.sendNotification(device, payload, false); // 发送后不要关闭连接

List<PushedNotification> notifications = new ArrayList<PushedNotification>();
if(notification!=null){
notifications.add(notification);
}

doAppleResponse(notifications);
} catch (Exception e) {
logger.error(e);
logger.error("error tokenID = "+token);
}
}

public void push2IOS2(String deviceToken, String message, Map<String, String> customParam){
javapns.back.PushNotificationManager pushManager2 = null;
String deviceId = null;
certificatePath = "zn_server.p12";
production = false;
certificatePassword = "123456";
try
{
String fullPath = PushMessageServiceImpl.class.getClassLoader().getResource(certificatePath).getPath();
PayLoad payLoad = new PayLoad();

payLoad.addAlert(message); //提醒文本
payLoad.addSound("default"); //提醒铃声
payLoad.addBadge(1);

this.addCustomDictionary2(payLoad, customParam);

deviceId = String.valueOf(payLoad.hashCode());
logger.info("Send notification for '" + deviceToken + ". PayLoad(" + payLoad.getPayloadAsBytes().length + " bytes) is:\n" + payLoad);
pushManager2 = javapns.back.PushNotificationManager.getInstance();
pushManager2.addDevice(deviceId, deviceToken);
logger.info("Add divice (id='" + deviceId + "').");
logger.info("Add divice (id='" + deviceId + "').");
pushManager2.initializeConnection("gateway.sandbox.push.apple.com", 2195, fullPath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);

logger.info("initializeConnection='" + "gateway.sandbox.push.apple.com" +" .. "+ 2195 +" .. "+fullPath +" .. "+certificatePassword + " ..");
javapns.data.Device client = pushManager2.getDevice(deviceId);
logger.info("Before send to " + deviceId + ".");
pushManager2.sendNotification(client, payLoad);
logger.info("Sended to " + deviceId + ".");
}catch(Exception e){
logger.error(e);
}
finally
{
try
{
pushManager2.stopConnection();
}
catch(Exception e)
{
logger.error("StopConnectionException", e);
}
try
{
pushManager2.removeDevice(deviceId);
}
catch(Exception e)
{
logger.error("RemoveDeviceException", e);
}
}
}

private Map<String, Boolean> doAppleResponse(List<PushedNotification> notifications){

List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications); // 失败通知
List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications); // 成功通知
int failed = failedNotifications.size();
int successful = successfulNotifications.size();
String result = (failed==0 ? "全部成功": (successful>0 ? "部分成功" : "全部失败") );
logger.info("doAppleResponse 结果: "+result+" failed :"+failed +"; successful:" + successful);

Map<String, Boolean> resultMap = new HashMap<String, Boolean>();

for(PushedNotification successfulNotification : successfulNotifications){
resultMap.put(successfulNotification.getDevice().getToken(), Boolean.TRUE);
logger.info("success Token:" + successfulNotification.getDevice().getToken()+" detail: "+successfulNotification);
logger.info("success notification detail: "+successfulNotification);
}

for(PushedNotification failedNotification : failedNotifications){
resultMap.put(failedNotification.getDevice().getToken(), Boolean.FALSE);
logger.info("failed Token:" + JSON.toJSONString(failedNotification.getDevice()));
logger.info("failed Exception:" + failedNotification.getException());
logger.info("failed LatestTransmissionAttempt:" + failedNotification.getLatestTransmissionAttempt());
}

logger.info("doAppleResponse resultMap: "+resultMap);

return resultMap;
}

@Override
public void doClose() throws Exception {

if(ansBasicImpl != null) {

try {
pushManager.stopConnection(); // 关闭与apple建立的连接
ansBasicImpl = null;
logger.info("=AppleApnsSAO:关闭与apple建立的连接成功");


} catch (Exception e) {
e.printStackTrace();
logger.error("Ios doClose =" +"Ios doClose :" + e);
} finally {
// 记录断开苹果服务器接口日志
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值