iphone推送java代碼實現

最近在研究iphone推送的java實現,看過發現原來很簡單,如下是我根據網上源碼修改的程序,具體裏面的證書和手機token的生產就不解釋了,本人沒有搞過iphone,有須要的能夠再網上搜索如下,不少也很簡單。java

代碼有不對的地方,多謝你們指出 幫忙改進。服務器

 /************************************************
 測試推送服務器地址:gateway.sandbox.push.apple.com /2195 
 產品推送服務器地址:gateway.push.apple.com / 2195 app

須要javaPNS_2.2.jar包iphone

 ***************************************************/測試

 /**debug

     *這是一個比較簡單的推送方法,token

     * apple的推送方法ip

     * @param tokens   iphone手機獲取的tokenget

     * @param path 這裏是一個.p12格式的文件路徑,須要去apple官網申請一個 源碼

     * @param password  p12的密碼 此處注意導出的證書密碼不能爲空由於空密碼會報錯

     * @param message 推送消息的內容

     * @param count 應用圖標上小紅圈上的數值

     * @param sendCount 單發仍是羣發  true:單發 false:羣發

     */

public void sendpush(List<String> tokens,String path, String password, String message,Integer count,boolean sendCount) {

try {

PushNotificationPayload payLoad = PushNotificationPayload.fromJSON(message);

//payLoad.addAlert(message); // 消息內容

payLoad.addBadge(count); // iphone應用圖標上小紅圈上的數值

payLoad.addSound("default"); // 鈴音 默認

PushNotificationManager pushManager = new PushNotificationManager();

//true:表示的是產品發佈推送服務 false:表示的是產品測試推送服務

pushManager.initializeConnection(new AppleNotificationServerBasicImpl(path, password, true));

List<PushedNotification> notifications = new ArrayList<PushedNotification>(); 

// 發送push消息

if (sendCount) {

log.debug("--------------------------apple 推送 單-------");

Device device = new BasicDevice();

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

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

notifications.add(notification);

} else {

log.debug("--------------------------apple 推送 羣-------");

List<Device> device = new ArrayList<Device>();

for (String token : tokens) {

device.add(new BasicDevice(token));

}

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();

 

if (successful > 0 && failed == 0) {

log.debug("-----All notifications pushed 成功 (" + successfulNotifications.size() + "):");

} else if (successful == 0 && failed > 0) {

log.debug("-----All notifications 失敗 (" + failedNotifications.size() + "):");

} else if (successful == 0 && failed == 0) {

System.out.println("No notifications could be sent, probably because of a critical error");

} else {

log.debug("------Some notifications 失敗 (" + failedNotifications.size() + "):");

log.debug("------Others 成功 (" + successfulNotifications.size() + "):");

}

// pushManager.stopConnection();

} catch (Exception e) {

e.printStackTrace();

}

}

相關文章
相關標籤/搜索