ios推送 java代码

来源:互联网 发布:sql server 日志备份 编辑:程序博客网 时间:2024/05/16 14:51
import java.util.ArrayList;import java.util.List;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.lang.StringUtils;public class ApnsSend{    public static void main(String[] args) throws Exception    {        // 向固定的设备发送        String deviceToken = "B5bd270c145936d9529c564f6c727de447698b01cf26ab57f90c205ca0fec793";//      List<String> deviceToken = new ArrayList<String>(); //多人发送//      deviveToken.add("");        //㈠设置消息内容        String alert = "ApnsSend!";        int badge = 100;//图标小红圈的数值        String sound = "default";//铃音                //㈡设置deviceToken 用户令牌        List<String> tokens = new ArrayList<String>();        tokens.add(deviceToken);     //把这个令牌号放入 令牌List中 ,因为有多个客户端 会有多个令牌号//      String certificatePath = "D:/certificate/证书.p12";//证书路径//      String certificatePath = "D:/certificate/Push.p12";//证书路径        String certificatePath = "D:\\pushApp.p12";//证书路径        String certificatePassword = "123";//此处注意导出的证书密码不能为空因为空密码会报错        boolean sendCount = true;      //true为 单人推送   flash为多人推送                //㈢将推送的内容 封装放入payLoad    javaPSN提供的简单易用的通知方式        try        {            PushNotificationPayload payLoad = new PushNotificationPayload();//PNPayload 用来装载push内容的类??            payLoad.addAlert(alert); // 消息内容            payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值            if (!StringUtils.isBlank(sound))//若为设置铃声  铃声就空缺着            {                payLoad.addSound(sound);//铃音            }                        //㈣初始化连接APNS  验证证书   flash:表示的是产品测试推送服务     若为true:表示的是产品发布推送服务            /**             *测试的服务器地址:gateway.sandbox.push.apple.com /端口2195             *产品推送服务器地址:gateway.push.apple.com / 2195             */             PushNotificationManager pushManager = new PushNotificationManager();            pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));            List<PushedNotification> notifications = new ArrayList<PushedNotification>();            // 消息 List            // ㈤发送push消息            if (sendCount) //单个推送            {                Device device = new BasicDevice();                device.setToken(tokens.get(0));                //取tokens  List中的第一个 token信息                PushedNotification notification = pushManager.sendNotification(device, payLoad, true);                //发送消息    包括token(device)  封装好的消息内容     后面的true???                notifications.add(notification);                  //将消息加入 notifications List            }            else // 群推送            {                List<Device> device = new ArrayList<Device>();                //建一个 List来存放 群组的token信息                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();            pushManager.stopConnection();//断开连接            System.out.println("成功发送:"+successful);            System.out.println("未发送     :"+failed);        }        catch (Exception e)        {            e.printStackTrace();        }    }}

0 0