標籤:group mat 表徵圖 state 推送認證 values param sem version
使用的是蘋果自己的推送伺服器
certificatePath 推送認證
VoipcertificatePath 喚醒認證
certificatePassword 認證密碼
以上三項都是需要使用上架了APP store的項目去申請認證,認證申請的步驟找度娘
需要的包
maven
<!-- 蘋果推送包 -->
<dependency>
<groupId>com.github.fernandospr</groupId>
<artifactId>javapns-jdk16</artifactId>
<version>2.4.0</version>
</dependency>
private static String certificatePath="E:/apache-tomcat-7.0.37/webapps/EstateService/IOSApp推送認證.p12"; //iOS開發人員憑證路徑,認證有發布認證和測試認證
private static String VoipcertificatePath="E:/apache-tomcat-7.0.37/webapps/EstateService/Voip推送認證.p12"; //iOS喚醒認證
private static String certificatePassword="123456"; //認證密碼
private static boolean production=false; //表示的是產品發布推送服務 false:表示的是產品測試推送服務
private static String VoipAppleUrl="gateway.sandbox.push.apple.com"; //喚醒伺服器位址 測試伺服器路徑:gateway.push.apple.com 發布產品伺服器路徑:gateway.sandbox.push.apple.com
private static int port=2195; //喚醒伺服器位址ip
/**
* 推送一個簡單訊息
* @param msg
* @param devices
* @throws CommunicationException
* @throws KeystoreException
*/
public void pushMsgNotification(String msg,List<String> tokens) throws CommunicationException, KeystoreException{
List<Device> devices = new ArrayList<Device>();
for (String token : tokens){
try {
devices.add(new BasicDevice(token));
} catch (InvalidDeviceTokenFormatException e) {
e.printStackTrace();
}
}
//推送返回結果集
List<PushedNotification> notifications = new ArrayList<PushedNotification>();
notifications= Push.alert(msg, certificatePath, certificatePassword, production, devices);
//擷取失敗結果集
List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications);
//擷取成功結果集
List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);
int failed = failedNotifications.size(); //推送失敗數
int successful = successfulNotifications.size(); //推送成功數
System.err.println("tokens:"+tokens);
System.err.println("失敗:"+failedNotifications);
System.err.println("蘋果推送失敗:"+failed+"條");
System.err.println("蘋果推送成功:"+successful+"條");
}
/**
* 推送一個alert+badge+sound通知或裝置喚醒推送
* @param tokens IOS tokens集合
* @param msg 推送訊息
* @param badge 表徵圖小紅圈的數值
* @param sound 鈴音
* @param isVoip 1:true喚醒推送 2:false訊息推送
*/
public void iOSpush(List<String> tokens,String msg,Integer badge,String sound,boolean isVoip){
try
{
PushNotificationBigPayload payLoad = new PushNotificationBigPayload();
payLoad.addAlert(msg); // 訊息內容
payLoad.addBadge(badge); // iphone應用表徵圖上小紅圈上的數值
payLoad.addSound(StringUtils.defaultIfEmpty(sound, "default"));//鈴音
PushNotificationManager pushManager = new PushNotificationManager();
//true:表示的是產品發布推送服務 false:表示的是產品測試推送服務
if(isVoip){
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(VoipcertificatePath, certificatePassword, ConnectionToAppleServer.KEYSTORE_TYPE_PKCS12, VoipAppleUrl, port));
}else{
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, production));
}
List<PushedNotification> notifications = new ArrayList<PushedNotification>();
// 發送push訊息
if (tokens.size()==1&&tokens.size()>0){
Device device = new BasicDevice();
device.setToken(tokens.get(0));
PushedNotification notification = pushManager.sendNotification(device, payLoad, true);
notifications.add(notification);
}else{
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(); //推送成功數
System.err.println("tokens:"+tokens);
System.err.println("失敗:"+failedNotifications);
System.err.println("蘋果推送失敗:"+failed+"條");
System.err.println("蘋果推送成功:"+successful+"條");
pushManager.stopConnection();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 推送自訂負載
* @param tokens集合
* @param msg 推送訊息
* @param badge 表徵圖小紅圈的數值
* @param sound 聲音
* @param map 擴充訊息
* @throws JSONException
* @throws CommunicationException
* @throws KeystoreException
*/
public void pushPayload(List<String> tokens, String msg,Integer badge,String sound,Map<String,String> map) throws JSONException, CommunicationException, KeystoreException{
PushNotificationBigPayload payload = customPayload(msg, badge, sound, map);
List<Device> devices = new ArrayList<Device>();
for (String token : tokens){
try {
devices.add(new BasicDevice(token));
} catch (InvalidDeviceTokenFormatException e) {
e.printStackTrace();
}
}
List<PushedNotification> notifications = new ArrayList<PushedNotification>();
notifications= Push.payload(payload, certificatePath, certificatePassword, production, devices);
List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications);
List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);
int failed = failedNotifications.size(); //推送失敗數
int successful = successfulNotifications.size(); //推送成功數
System.err.println("tokens:"+tokens);
System.err.println("失敗:"+failedNotifications);
System.err.println("蘋果推送失敗:"+failed+"條");
System.err.println("蘋果推送成功:"+successful+"條");
}
/**
* 推送自訂喚醒負載
* @param tokens集合
* @param msg 推送訊息
* @param badge 表徵圖小紅圈的數值
* @param sound 聲音
* @param map 擴充訊息
* @throws JSONException
* @throws CommunicationException
* @throws KeystoreException
*/
public void voippushPayload(List<String> tokens, String msg,Integer badge,String sound,Map<String,String> map) throws JSONException, CommunicationException, KeystoreException{
try {
//封裝擴張訊息
PushNotificationBigPayload payload = customPayload(msg, badge, sound, map);
//建立Devices集合存放,裝置
List<Device> devices = new ArrayList<Device>();
for (String token : tokens){
devices.add(new BasicDevice(token));
}
//iOS推送
PushNotificationManager pushManager = new PushNotificationManager();
//推送方式
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(VoipcertificatePath, certificatePassword, ConnectionToAppleServer.KEYSTORE_TYPE_PKCS12, VoipAppleUrl, port));
//存放推送放回對象集合
List<PushedNotification> notifications = new ArrayList<PushedNotification>();
// 發送push訊息
notifications = pushManager.sendNotifications(payload, devices);
List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications);
List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);
int failed = failedNotifications.size(); //推送失敗數
int successful = successfulNotifications.size(); //推送成功數
System.err.println("tokens:"+tokens);
System.err.println("失敗:"+failedNotifications);
System.err.println("蘋果推送失敗:"+failed+"條");
System.err.println("蘋果推送成功:"+successful+"條");
} catch (InvalidDeviceTokenFormatException e) {
e.printStackTrace();
}
}
/**
* 自訂負載
* @param msg
* @param badge
* @param sound
* @param map 自訂字典
* @return
* @throws JSONException
*/
private PushNotificationBigPayload customPayload(String msg,Integer badge,String sound,Map<String,String> map) throws JSONException{
PushNotificationBigPayload payload = PushNotificationBigPayload.complex();
if(StringUtils.isNotEmpty(msg)){
payload.addAlert(msg);
}
if(badge != null){
payload.addBadge(badge);
}
payload.addSound(StringUtils.defaultIfEmpty(sound, "default"));
if(map!=null && !map.isEmpty()){
Object[] keys = map.keySet().toArray();
Object[] vals = map.values().toArray();
if(keys!= null && vals != null && keys.length == vals.length){
for (int i = 0; i < map.size(); i++) {
payload.addCustomDictionary(String.valueOf(keys[i]),String.valueOf(vals[i]));
}
}
}
return payload;
}
蘋果pns推送和喚醒