iphone推送

1.iphone推送
与iphone推送相关的网站:
http://code.google.com/p/javapns/

编写发送通知的简单实例

第三方依赖包(下载在下面):
log4j-1.2.16.jar
javaPNS_2.2.jar
java代码:

package com.sdunisi.iphone.apns.send;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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;
public class MainApnsSend {
public static void main(String[] args) throws Exception {
try {
String deviceToken= = "e775b5892f3334427c14def8aa4d8189a4ec1c795020072f4baa7ee92e50b1db";//iphone手机获取的token
//message是一个json的字符串{“aps”:{“alert”:”iphone推送测试”}}
PushNotificationPayload payLoad = PushNotificationPayload.fromJSON(message); 	
payLoad.addBadge(1); // 图标小红圈的数值
payLoad.addSound("default"); // 铃音 默认
PushNotificationManager pushManager = new PushNotificationManager();
//Connect to APNs
/**
*测试的服务器地址:gateway.sandbox.push.apple.com /端口2195
*产品推送服务器地址:gateway.push.apple.com / 2195
*/
String path= "/Users/jcjc/Desktop/push.p12";//导出的证书
String password= "sunlg";//此处注意导出的证书密码不能为空因为空密码会报错
pushManager.initializeConnection(new AppleNotificationServerBasicImpl(path, password,  false));//true:表示产品推送服务器 false:表示测试的服务器
List<PushedNotification> notifications = new ArrayList<PushedNotification>(); 
// 发送push消息 sendCount 是个boolean类型的值用来区分单发还是群发 true:单发 false:群发
if (sendCount) {
log.debug("--------------------------apple 推送 单-------"+tokens.get(0));
Device device = new BasicDevice();
device.setToken(tokens.get(0));
PushedNotification notification = pushManager.sendNotification(device, payLoad, true);
notifications.add(notification);
} else {
log.debug("--------------------------apple 推送 群-------"+tokens.size());
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() + "):");
}
} catch (Exception e) {
	e.printStackTrace();
}

你可能感兴趣的:(iPhone)