java給iphone應用實現推送

來源:互聯網
上載者:User

根據公司項目所需,需要對iphone應用進行訊息推送,一開始選的是php,但是php語言知識略懂,開發起來比較麻煩,所有就用比較熟悉的java語言進行訊息推送。

需要依賴的jar包:

 

具體代碼如下:

/** * MainSend.java * 著作權(C) 2012  * 建立:cuiran 2012-07-24 11:31:35 */package com.wpn.iphone.send;import java.util.ArrayList;import java.util.List;import javapns.devices.Device;import javapns.devices.implementations.basic.BasicDevice;import javapns.notification.AppleNotificationServerBasicImpl;import javapns.notification.PushNotificationManager;import javapns.notification.PushNotificationPayload;import javapns.notification.PushedNotification;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * TODO * @author cuiran * @version TODO */public class MainSend {private static Log log = LogFactory.getLog(MainSend.class.getName()); /************************************************ 測試推送伺服器位址:gateway.sandbox.push.apple.com /2195  產品推送伺服器位址:gateway.push.apple.com / 2195 需要javaPNS_2.2.jar包 ***************************************************//**     *這是一個比較簡單的推送方法,     * apple的推送方法     * @param tokens   iphone手機擷取的token     * @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 {//message是一個json的字串{“aps”:{“alert”:”iphone推送測試”}}PushNotificationPayload payLoad =  PushNotificationPayload.fromJSON(message);payLoad.addAlert("iphone推送測試 www.baidu.com"); // 訊息內容payLoad.addBadge(count); // iphone應用表徵圖上小紅圈上的數值payLoad.addSound("default"); // 鈴音 預設PushNotificationManager pushManager = new PushNotificationManager();//true:表示的是產品發布推送服務 false:表示的是產品測試推送服務pushManager.initializeConnection(new AppleNotificationServerBasicImpl(path, password, false));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();}}/** * TODO * @param args */public static void main(String[] args) {MainSend send=new MainSend();List<String> tokens=new ArrayList<String>();tokens.add("76edc85fd2e6704b27974d774cc046d7e33a3440fd6f39ba18c729387e6c788a");tokens.add("dc2cf037bd4465c851b1d96a86b0a028307bc7e443435b6fafe93c2957bb415c");String path="E:\\iphone\\WPNPushService.p12";String password="wappin2009";String message="{'aps':{'alert':'iphone推送測試 www.baidu.com'}}";Integer count=1;boolean sendCount=false;send.sendpush(tokens, path, password, message, count, sendCount);}}

源碼:  點擊開啟連結

轉自:http://blog.csdn.net/cuiran/article/details/7801280

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.