[ 转 ] Java实现IOS推送(Javapns2.2)

原文地址:http://www.cnblogs.com/lihaozy/archive/2013/03/13/2957904.html

此程序需要Javapns 2.2版本。

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 
 4 import javapns.Push;
 5 import javapns.devices.Device;
 6 import javapns.devices.implementations.basic.BasicDevice;
 7 import javapns.notification.AppleNotificationServerBasicImpl;
 8 import javapns.notification.PushNotificationManager;
 9 import javapns.notification.PushNotificationPayload;
10 import javapns.notification.PushedNotification;
11 import org.apache.commons.lang.StringUtils;
12 
13 public class ApnsSend
14 {
15     public static void main(String[] args) throws Exception
16     {
17         BasicConfigurator.configure();
18         String deviceToken = "d7e6132895b388cf016433167c9e2d97fe4b76ca5a1692209a3b6e3cb3fdcd9c";
19         String alert = "我的push测试";//push的内容
20         int badge = 100;//图标小红圈的数值
21         String sound = "default";//铃音
22 
23         List<String> tokens = new ArrayList<String>();
24         tokens.add(deviceToken);
25         String certificatePath = "D:/PushDev.p12";
26         String certificatePassword = "123456";//此处注意导出的证书密码不能为空因为空密码会报错
27         boolean sendCount = true;
28 
29         try
30         {
31             PushNotificationPayload payLoad = new PushNotificationPayload();
32             payLoad.addAlert(alert); // 消息内容
33             payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值
34             if (!StringUtils.isBlank(sound))
35             {
36                 payLoad.addSound(sound);//铃音
37             }
38             PushNotificationManager pushManager = new PushNotificationManager();
39             //true:表示的是产品发布推送服务 false:表示的是产品测试推送服务
40             pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));
41             List<PushedNotification> notifications = new ArrayList<PushedNotification>();
42             // 发送push消息
43             if (sendCount)
44             {
45                 Device device = new BasicDevice();
46                 device.setToken(tokens.get(0));
47                 PushedNotification notification = pushManager.sendNotification(device, payLoad, true);
48                 notifications.add(notification);
49             }
50             else
51             {
52                 List<Device> device = new ArrayList<Device>();
53                 for (String token : tokens)
54                 {
55                     device.add(new BasicDevice(token));
56                 }
57                 notifications = pushManager.sendNotifications(payLoad, device);
58             }
59             List<PushedNotification> failedNotifications = PushedNotification.findFailedNotifications(notifications);
60             List<PushedNotification> successfulNotifications = PushedNotification.findSuccessfulNotifications(notifications);
61             int failed = failedNotifications.size();
62             int successful = successfulNotifications.size();
63             pushManager.stopConnection();
64         }
65         catch (Exception e)
66         {
67             e.printStackTrace();
68         }
69     }
70 }

 

posted @ 2013-04-17 17:50  chemandy  阅读(276)  评论(0)    收藏  举报