IOS消息推送服务端开发

来源:互联网 发布:靠谱的分装小样店 知乎 编辑:程序博客网 时间:2024/06/05 12:46

IOS消息推送服务端开发

ios消息推送主要主流有两种方式,一种是基于javaapns.jar和javaPNS.jar的开源的消息推送,javaPNS.jar支持多线程,工程结构如下:

源码下载如下(仅供参考):
http://download.csdn.net/detail/liaochangbin09/7211917

第一种:基于javaapns.jar的实现如下:
package com.ios.ApnsSend;import java.util.List;import javapns.back.PushNotificationManager;import javapns.back.SSLConnectionHelper;import javapns.data.Device;import javapns.data.PayLoad;import com.ios.domain.PushInfo;public class MulApnsSend {/** *  * @param filePath 证书文件路径 * @param password 密码 * @param content 消息内容 * @param pushInfos 推送消息集合 */public static void pushToClients(String filePath,String password,String content,List<PushInfo> pushInfos) {System.out.println("to pushs");try {PayLoad payLoad=new PayLoad();//消息内容payLoad.addAlert(content);payLoad.addBadge(1);//应用图标上小红圈上的数值     ////铃声默认payLoad.addSound("default");PushNotificationManager pushManager=PushNotificationManager.getInstance();String testHost= "gateway.sandbox.push.apple.com";  String realHost="gateway.push.apple.com";//链接到APNs      pushManager.initializeConnection(testHost, 2195,     filePath, password, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);         for (int i = 0; i < pushInfos.size(); i++) {    System.out.println("iphone" + pushInfos.get(i).getId());    pushManager.addDevice("iphone" + pushInfos.get(i).getId(), pushInfos.get(i).getToken());         Device client = pushManager.getDevice("iphone"+ pushInfos.get(i).getId());          pushManager.sendNotification(client, payLoad);  }        pushManager.stopConnection();      for (int i = 0; i < pushInfos.size(); i++) {      pushManager.removeDevice("iphone" + pushInfos.get(i).getId());    }    } catch (Exception e) {// TODO: handle exceptione.printStackTrace();System.out.println(e.getMessage());}}}
第二种方式:基于javaPNS.jar的ios消息推送机制
package com.ios.PnsSend;import java.io.File;import java.util.ArrayList;import java.util.List;import com.ios.domain.PushInfo;import javapns.notification.AppleNotificationServer;import javapns.notification.AppleNotificationServerBasicImpl;import javapns.notification.PayloadPerDevice;import javapns.notification.PushNotificationPayload;import javapns.notification.PushedNotification;import javapns.notification.transmission.NotificationProgressListener;import javapns.notification.transmission.NotificationThread;import javapns.notification.transmission.NotificationThreads;public class PnsSend {/** * 基于PNS的消息推送机制 * @param keystore 证书路径和证书名   * @param password 证书密码   * @param production 设置true为正式服务地址,false为开发者地址 * @param pushInfos */public static void pushToClient(String keystore,String password,boolean production,List<PushInfo> pushInfos) {    int threadThreads = 10; // 线程数  try {    // 建立与Apple服务器连接      AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production);      List<PayloadPerDevice> list = new ArrayList<PayloadPerDevice>();      for (PushInfo pushInfo : pushInfos) {    PushNotificationPayload payload = new PushNotificationPayload();      payload.addAlert(pushInfo.getAlert());      payload.addSound("default");// 声音      payload.addBadge(1);//图标小红圈的数值      payload.addCustomDictionary(pushInfo.getCustomDictionaryKey(),pushInfo.getCustomDictionaryValue());// 添加字典       PayloadPerDevice pay = new PayloadPerDevice(payload,pushInfo.getToken());// 将要推送的消息和手机唯一标识绑定      list.add(pay);  }    NotificationThreads work = new NotificationThreads(server,list,threadThreads);//       work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听      work.start(); // 启动线程      work.waitForAllThreads();// 等待所有线程启动完成     } catch (Exception e) {      e.printStackTrace();   }}  // 线程监听  public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {          public void eventThreadStarted(NotificationThread notificationThread) {              System.out.println("   [EVENT]: thread #" + notificationThread.getThreadNumber() + " started with " + " devices beginning at message id #" + notificationThread.getFirstMessageIdentifier());          }          public void eventThreadFinished(NotificationThread thread) {              System.out.println("   [EVENT]: thread #" + thread.getThreadNumber() + " finished: pushed messages #" + thread.getFirstMessageIdentifier() + " to " + thread.getLastMessageIdentifier() + " toward "+ " devices");          }          public void eventConnectionRestarted(NotificationThread thread) {              System.out.println("   [EVENT]: connection restarted in thread #" + thread.getThreadNumber() + " because it reached " + thread.getMaxNotificationsPerConnection() + " notifications per connection");          }          public void eventAllThreadsStarted(NotificationThreads notificationThreads) {              System.out.println("   [EVENT]: all threads started: " + notificationThreads.getThreads().size());          }          public void eventAllThreadsFinished(NotificationThreads notificationThreads) {              System.out.println("   [EVENT]: all threads finished: " + notificationThreads.getThreads().size());          }          public void eventCriticalException(NotificationThread notificationThread, Exception exception) {              System.out.println("   [EVENT]: critical exception occurred: " + exception);          }       };  }
PushInfo.java代码如下:
package com.ios.domain;public class PushInfo {//idprivate int id;//手机的唯一标识private String token;//推送内容private String alert;//自定义keyprivate String customDictionaryKey;//自定义valueprivate String customDictionaryValue;public String getCustomDictionaryKey() {return customDictionaryKey;}public void setCustomDictionaryKey(String customDictionaryKey) {this.customDictionaryKey = customDictionaryKey;}public String getCustomDictionaryValue() {return customDictionaryValue;}public void setCustomDictionaryValue(String customDictionaryValue) {this.customDictionaryValue = customDictionaryValue;}public String getAlert() {return alert;}public void setAlert(String alert) {this.alert = alert;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getToken() {return token;}public void setToken(String token) {this.token = token;}}
测试代码如下:
package com.ios.test;import java.io.File;import java.util.ArrayList;import java.util.List;import com.ios.ApnsSend.MulApnsSend;import com.ios.PnsSend.PnsSend;import com.ios.domain.PushInfo;public class IosPushSendTest {public static void main(String[] args) {//第一种:基于APNS的ios消息推送测试List<PushInfo> pushInfos=new ArrayList<PushInfo>();PushInfo pushInfo1 = new PushInfo();pushInfo1.setId(1);pushInfo1.setToken("ff3a3ac5 6e3d3105 3df8f3b4 9f3f97df b51b35af 2a86ac41 df40cc35 3e5730dd");pushInfos.add(pushInfo1);PushInfo pushInfo2 = new PushInfo();pushInfo2.setId(2);pushInfo2.setToken("9689ee7a 4625ccc6 296a8957 ecff5f31 3b512999 e9bec18e c5222b1e e871ab12");pushInfos.add(pushInfo2);String path=System.getProperty("user.dir")+File.separator+"file"+File.separator+"apns.p12";MulApnsSend.pushToClients(path, "123456", "你大爷。", pushInfos);//第二种:基于PNS的ios消息推送测试//推送内容:String content="长腿欧巴到啦。";//证书路径和证书名      String keystore=System.getProperty("user.dir")+File.separator+"file"+File.separator+"apns.p12";    String password = "123456"; // 证书密码      // 手机唯一标识      List<PushInfo> pushInfosList=new ArrayList<PushInfo>();    //第一个手机需要发送的信息如下:    PushInfo pnsPushInfo1 = new PushInfo();    pnsPushInfo1.setId(1);    pnsPushInfo1.setToken("ff3a3ac56e3d31053df8f3b49f3f97dfb51b35af2a86ac41df40cc353e5730dd");    pnsPushInfo1.setAlert(content);    pnsPushInfo1.setCustomDictionaryKey("url");    pnsPushInfo1.setCustomDictionaryValue("http://www.baidu.com/");    pushInfosList.add(pnsPushInfo1);    //第二个手机需要发送的信息如下:    PushInfo pnsPushInfo2 = new PushInfo();    pnsPushInfo2.setId(2);    pnsPushInfo2.setToken("9689ee7a4625ccc6296a8957ecff5f313b512999e9bec18ec5222b1ee871ab12");    pnsPushInfo2.setAlert(content);    pnsPushInfo2.setCustomDictionaryKey("url");    pnsPushInfo2.setCustomDictionaryValue("http://www.baidu.com/");    pushInfosList.add(pnsPushInfo2);    // 设置true为正式服务地址,false为开发者地址     boolean production = false;     PnsSend.pushToClient(keystore, password, production,pushInfosList);}}


0 0
原创粉丝点击