IOS推送消息(java实现)

1  首先在mac系统下制作用于推送的证书,然后导入钥匙串。证书制作过程这里不在叙述。将证书和专有密钥一起导出,输入密码。然后导出,命名aaa.p12文件,将文件拷贝出来,放到windows下。

 

2 打开eclipse,编写java代码。

 

import java.util.HashMap;
import java.util.Iterator;

 


import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;

public class MainApnsSend {

    public static void main(String[] args) throws Exception {
       
       try {
              String deviceToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

              //被推送的iphone应用程序标示符      
              PayLoad payLoad = new PayLoad();
              payLoad.addAlert("测试我的push消息");
              payLoad.addBadge(1);
              payLoad.addSound("default");
                       
              PushNotificationManager pushManager = PushNotificationManager.getInstance();
              pushManager.addDevice("iphone", deviceToken);
             
           //Device c = pushManager.getDevice("iphone");
              String host= "gateway.sandbox.push.apple.com";  //测试用的苹果推送服务器

              int port = 2195;
              String certificatePath = "D:/aaa.p12"; //刚才在mac系统下导出的证书
              
              String certificatePassword= "841112";
             
              pushManager.initializeConnection(host, port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);//初始化tcp连接,公司网络代理上网,不能连上外网的tcp连接,坑死人
                        
              //Send Push
              Device client = pushManager.getDevice("iphone");
              pushManager.sendNotification(client, payLoad); //推送消息
              pushManager.stopConnection();

              pushManager.removeDevice("iphone");
             }
             catch (Exception e) {
              e.printStackTrace();
             }
             System.out.println("push succeed!");

    }
}

需要用到的jar包

bcprov-jdk16-145-1.jar

commons-io-2.0.1.jar

commons-lang-2.5.jar

javapns-jdk16-163.jar

log4j-1.2.16.jar


你可能感兴趣的:(IOS推送消息(java实现))