mac下使用java测试iOS推送

首先mac下有很多现在的测试iOS推送软件,为什么要用java程序测试呢;

因为大多数后台推送服务可能是JAVA开发的,那么为了验证我们在MAC上导出的推送证书文件是否正确;

制作开发证书的iOS开发人员,应当用JAVA自测来保证导出的p12推送证书文件是正确的;

1. iOS开发人员从mac钥匙串导出p12格式的推送证书;

2. mac环境配置java

   首先安装java,很简单从官方下载dmg格式的java sdk,安装即可;

   测试程序需要一些java的库,即jar包;以下我测试ok用到的jar包,

   可直接百度对就的名字下载,或从对应的官网下载:大致用到 jackson,javapns,log4j 这三个包

    jackson-core-2.9.9.jar

  javapns-jdk16-2.4.0.jar

  apache-log4j-2.12.0.jar(这个可能是好几个log4j的jar)

3. 安装依赖的jar包

   mac下java的包安装目录在  /Library/Java/Extensions/

   我们把上面下载的jar包 放在上面目录即可;

4. 测试的java程序代码

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;

/***
 * 依赖的jar包有 
 jackson-core-2.9.9.jar 
 javapns-jdk16-2.4.0.jar
 apache-log4j-2.12.0.jar
 * 
 * //mac下安装的路径到 /Library/Java/Extensions/目录下
 * 
 * //测试 javac PushMsg.java java PushMsg
 * 
 */

public class PushMsg {
        public static void main(String[] args) throws Exception {

                System.out.println("zsl==========开始推送消息");
                int badge = 1; // 图标小红圈的数值
                String sound = "default"; // 铃音
                // 要推送的,手机设备token号
                String deviceToken = "753c86b495613089f02dcd3f735f0ada9e2d40f84c0a6360802ea57e55f43b8x";
                // 这里是要推送的测试消息
                String message = "test push message to ios device";

                List<String> tokens = new ArrayList<String>();
                tokens.add(deviceToken);

                // java必须要用导出p12文件 ,php的话是pem文件
                // 注意证书是生产环境还是测试环境
                String certificatePath = "./APNS_iOS_3.p12";
                // 从mac钥匙串,导出证书时设置的密码
                String msgCertificatePassword = "1";

                boolean sendCount = true;

                PushNotificationPayload payload = new PushNotificationPayload();
                payload.addAlert(message); // 消息内容
                payload.addBadge(badge);
                payload.addCustomDictionary("uid", "haahi");
                payload.addCustomDictionary("type", 12);
                payload.addCustomDictionary("title", "haahi");
                payload.addSound("default.caf");// 铃音

                PushNotificationManager pushManager = new PushNotificationManager();
                // true:对应iOS生产环境推送 false:对应iOS测试环境推送
                pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, msgCertificatePassword, true));
                List<PushedNotification> notifications = new ArrayList<PushedNotification>();
                // 开始推送消息
                if (sendCount) {
                        Device device = new BasicDevice();
                        device.setToken(deviceToken);
                        PushedNotification notification = pushManager.sendNotification(device, payload, false);
                        notifications.add(notification);
                } else {
                        List<Device> devices = new ArrayList<Device>();
                        for (String token : tokens) {
                                devices.add(new BasicDevice(token));
                        }
                        notifications = pushManager.sendNotifications(payload, devices);
                }

                List<PushedNotification> failedNotification = PushedNotification.findFailedNotifications(notifications);
                List<PushedNotification> successfulNotification = PushedNotification
                                .findSuccessfulNotifications(notifications);
                int failed = failedNotification.size();
                int successful = successfulNotification.size();
                System.out.println("zsl==========成功数:" + successful);
                System.out.println("zsl==========失败数:" + failed);
                pushManager.stopConnection();
                System.out.println("zsl==========消息推送完毕");
        }
}

  

代码里面都有注释

需要的注意的是 上面推送手机的token,推送证书的路径,推送证书的密码,推送证书类型生产还是测试;

5. 运行测试

  在mac终端下先用javac编译

   javac PushMsg.java

  然后运行生成的PushMsg.class

   java PushMsg

  在终端看日志,以及手机接收到通知来验证;

ccMBP:20190726javaPush cc$ javac PushMsg.java 
ccMBP:20190726javaPush cc$ java PushMsg
zsl==========开始推送消息
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
zsl==========成功数:1
zsl==========失败数:0
zsl==========消息推送完毕

  

     

推送代码参考:https://www.jianshu.com/p/7a9f544a1ae3

原文地址:https://www.cnblogs.com/cocoajin/p/11250413.html