Java實現IOS推送(利用javapns1.6)

weixin_34402090發表於2013-03-13

 

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

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
 

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 = "d7e6132895b388cf016433167c9e2d97fe4b76ca5a1692209a3b6e3cb3fdcd9c";
            
            
            //被推送的iphone應用程式標示符      
            PropertyConfigurator.configure("bin/log4j.properties");
            Logger console = Logger.getLogger(MainApnsSend.class);
              
            PayLoad payLoad = new PayLoad();
            payLoad.addAlert("彩通萬歲!OLALA!!AAAA");
            payLoad.addBadge(1);
            payLoad.addSound("default");
            
            PushNotificationManager pushManager = PushNotificationManager.getInstance();
            pushManager.addDevice("iPhone", deviceToken);
            
            String host= "gateway.sandbox.push.apple.com";  //測試用的蘋果推送伺服器
            int port = 2195;
            String certificatePath = "D:/PushDev.p12"; //剛才在mac系統下匯出的證照
              
            String certificatePassword= "123456";
            
            pushManager.initializeConnection(host, port, certificatePath,certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
              
            //Send Push
            Device client = pushManager.getDevice("iPhone");
            pushManager.sendNotification(client, payLoad); 
            pushManager.stopConnection();
            pushManager.removeDevice("iPhone");
            System.out.println("push succeed!");
        }
        catch (Exception e) {
            //e.printStackTrace();
            System.out.println("e.getMessage() = " + e.getMessage());
        }
             
    }
}

 

需要用到的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

 

 

 

相關文章