使用的是苹果自己的推送服务器

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推送和唤醒的更多相关文章

  1. .NET向APNS苹果消息推送通知

    一.Apns简介: Apns是苹果推送通知服务. 二.原理: APNs会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书以确定其为苹果设备):然后,将服务器的信息接收并且 ...

  2. PHP 苹果消息推送

    /* * 苹果消息推送方法 * $deviceToken 苹果设备token * $message 消息内容 */ function iosmsg_send($deviceToken,$message ...

  3. .net C# 苹果消息推送 工具类

    public class AppleAPNSMessage { /// <summary> /// 苹果信息推送 证书 路径(注意测试版跟正式发布版证书上不一样) /// </sum ...

  4. 手把手教你配置苹果APNS推送服务|钿畑的博客 | 钿畑的博客

    http://www.360doc.com/content/15/0118/17/1073512_441822850.shtml# 钿畑的文章索引 1. 什么是推送通知 2. 什么是APNS? 3. ...

  5. ZPush--基于netty4实现的苹果通知推送服务(APNs)Javaclient

    简单说下实现苹果通知推送服务(APNs)client的一些要注意的地方: 使用长连接: sanboxserver是无用的,调试时直接用"gateway.push.apple.com" ...

  6. 苹果开始推送 macOS Catalina10.15 正式版系统更新

    北京时间今天凌晨 1 点,苹果正式推送了 macOS Catalina 10.15 正式版升级.macOS Catalina 10.15 正式版带来了许多重大改变,包括Sidecar.iTunes应用 ...

  7. 苹果通知推送服务(APNS)一些关键特性摘要

    http://ramosli.iteye.com/blog/1940843 前段时间,仔细研究了APNS的文档,把一些关键的地方记录了下来,弄懂这些对于理解APNS的规则,至关重要. 1. If AP ...

  8. 苹果通知推送服务(APNS)关键特性摘要

    1. If APNs attempts to deliver a notification but the device is offline, the notification is stored ...

  9. (转)苹果消息推送服务器 php 证书生成

    1.准备好 aps_developer_identity.cer , push.p12这两个证书文件 2. 生成证书如下: openssl x509 -in aps_developer_identit ...

随机推荐

  1. FSMC_LCD

    1. TFT-LCD(Thin Film Transistor Liquid Crystal Display)[薄膜晶体管液晶显示器] 2. 液晶 物质在熔融状态或在溶液状态下虽然获得了液体物质的流动 ...

  2. iOS开发 -------- 网络状态监测

    一 示例代码 需要先把第三方Reachability下载导入到工程中  下载网址  https://github.com/tonymillion/Reachability 1 封装网络工具类 Netw ...

  3. RabbitMQ&RocketMQ动态添加Queue参考

    Kafka重复消费与消息丢失参考: https://www.cnblogs.com/kaleidoscope/p/9763053.html https://blog.csdn.net/qingqing ...

  4. Learning-Python【8】:Python字符编码

    1.内存和硬盘都是用来存储的 内存:速度快 硬盘:永久保存 2.文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就可以启动一个进程,是在内存中的,所以在编辑器编写的 ...

  5. mui框架下监听返回按钮

    用于监听mui框架下的Android手机的返回按键(物理键) mui.back = function() { if(b == true) {//一个标识符,在某个状态下不允许双击返回关闭程序 aler ...

  6. Linux修改磁盘挂载目录

    比如想把已经挂载在home目录上的硬盘挂载到data目录上, 如下操作 #df -h(查看分区情况及数据盘名称) # mkdir /data(如果没有data目录就创建,否则此步跳过) # umoun ...

  7. Codeforces Round #113 (Div. 2) B. Polygons Andrew求凸包

    B. Polygons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  8. win平台下Path变量消失问题

    解决方法:2019.01.10文章转载自 李北北:https://www.jianshu.com/p/b89f0c99867e 问题描述:修改了path变量,但是环境变量中path消失,于是想再次打开 ...

  9. 深入理解Plasma(二)Plasma 细节

    这一系列文章将围绕以太坊的二层扩容框架,介绍其基本运行原理,具体操作细节,安全性讨论以及未来研究方向等.本篇文章主要对 Plasma 一些关键操作的细节进行剖析. 在上一篇文章中我们已经理解了什么是 ...

  10. three.js 第二篇:场景 相机 渲染器 物体之间的关系

    w我用画画来形容他们之间的关系 场景就是纸张 相机就是我们的眼睛 物体就是在我们脑海中构思的那个画面 渲染器就是绘画这个动作 场景(Scene): 初始化:var scene = new THREE. ...