苹果pns推送和唤醒

Posted 天霸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了苹果pns推送和唤醒相关的知识,希望对你有一定的参考价值。

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

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推送和唤醒的主要内容,如果未能解决你的问题,请参考以下文章

黑苹果休眠后唤醒黑屏只有鼠标能动

远程推送通知不会唤醒我的手机

iOS 本地推送通知未唤醒设备

苹果电脑休眠怎么唤醒

苹果手机可以安装唤醒软件不

使用firebase发送静默推送通知,以便在应用程序被杀死时唤醒它