java给iphone推送

需要包:bcprov-jdk15-143.jar、JavaPNS_2.2.jar

生成:服务器端所用的.p12文件(.net或Java等后台,php 是.pem文件)


/**


    * @param tokens   iphone手机获取的token


    * @param path 这里是一个证书格式的文件路径,需要去apple官网申请一个 


    * @param password  p12的密码 此处注意导出的证书密码不能为空因为空密码会报错


    * @param message 推送消息的内容 message是一个json的字符串{“aps”:{“alert”:”iphone推送测试”}} 格式


    * @param count 应用图标上小红圈上的数值


    * @param sendCount 单发还是群发  true:单发  false:群发
    * 
    * @param iosPushRelease 正式发布 证书 true,测试发布证书  false:


    */
public static Boolean sendpush(List tokens, String message,int count,boolean sendCount,boolean iosPushRelease) {
boolean result = false;
try {
String password= "";
String path = "";
PushNotificationPayload payLoad =  PushNotificationPayload.fromJSON(message);


// PushNotificationPayload payLoad =  PushNotificationPayload.alert(message);
// payLoad.addCustomAlertBody(message); 
payLoad.addBadge(count); // iphone应用图标上小红圈上的数值
payLoad.addSound("default"); // 铃音 默认
//添加字典  
PushNotificationManager pushManager = new PushNotificationManager();


//链接到APNs  true:表示的是产品发布推送服务 false:表示的是产品测试推送服务
if(iosPushRelease){
// path = Settings.MY_HOST_IP + "QNfiles/develop.pem";
path = Settings.MY_TOMCAT_PATH + "/QNfiles/develop.pem";
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(path, password, true));
}else{
// path =  Settings.MY_HOST_IP + "QNfiles/release.pem";
path = Settings.MY_TOMCAT_PATH + "/QNfiles/Push.p12";
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(path, password, false));
}

List notifications = new ArrayList(); 

// 开始推送   发送push消息

if (sendCount) {
Device device = new BasicDevice();

device.setToken(tokens.get(0));

PushedNotification notification = pushManager.sendNotification(device, payLoad, true);

notifications.add(notification);

} else {
List device = new ArrayList();

for (String token : tokens) {

device.add(new BasicDevice(token));

}

notifications = pushManager.sendNotifications(payLoad, device);

}

List failedNotifications = PushedNotification.findFailedNotifications(notifications);

List successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);

int failed = failedNotifications.size();

int successful = successfulNotifications.size();

if (successful > 0 && failed == 0) {
result = true;
System.out.print("推送成功"+failedNotifications.toString());

} else if (successful == 0 && failed > 0) {
 System.out.print("推送失败"+failedNotifications.toString());

} else if (successful == 0 && failed == 0) {
System.out.print("推送失败"+failedNotifications.toString());
   System.out.println("No notifications could be sent, probably because of a critical error");

} else {
System.out.print("推送失败"+failedNotifications.toString());

}
//断开链接  
//    pushManager.stopConnection();  
} catch (Exception e) {

e.printStackTrace();

}

return result;
}


PushNotificationPayload 有两种方式等同:

1>PushNotificationPayload payLoad =  PushNotificationPayload.fromJSON(message);

该方法message格式为json

 {
  "aps":
  {
    "alert": "Breaking News!",
    "sound": "default"
  }
 "" : ""


2>PushNotificationPayload payLoad =  PushNotificationPayload.alert(message);

该方法message是通知栏的内容

还需其他设置,如:payLoad.addBadge(count); // iphone应用图标上小红圈上的数值
payLoad.addSound("default"); // 铃音 默认;payLoad.addCustomAlertBody(message);

你可能感兴趣的:(java)