mac下使用java測試iOS推送

首先mac下有不少如今的測試iOS推送軟件,爲何要用java程序測試呢;php

由於大多數後臺推送服務多是JAVA開發的,那麼爲了驗證咱們在MAC上導出的推送證書文件是否正確;html

製做開發證書的iOS開發人員,應當用JAVA自測來保證導出的p12推送證書文件是正確的;java

 

1. iOS開發人員從mac鑰匙串導出p12格式的推送證書;ios

2. mac環境配置javaapache

   首先安裝java,很簡單從官方下載dmg格式的java sdk,安裝便可;bash

   測試程序須要一些java的庫,即jar包;如下我測試ok用到的jar包,ide

   可直接百度對就的名字下載,或從對應的官網下載:大體用到 jackson,javapns,log4j 這三個包測試

    jackson-core-2.9.9.jarui

  javapns-jdk16-2.4.0.jardebug

  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

相關文章
相關標籤/搜索