Java test using iOS push under mac

First, there are a lot of mac now under test push iOS software, why use java program to test it;

Because most background push service could be developed in JAVA, so we exported in order to verify the MAC push certificate file is correct;

Create a Developer Certificate of iOS developers, self-test should be used to ensure that the exported JAVA p12 push certificate file is correct;

 

1. iOS developer p12 certificate format string export push from mac key;

2. mac java environment configuration

   First install java, is very simple to download the java sdk dmg format from the official, can be installed;

   The test program requires some java libraries that jar package; I tested the following jar pack ok used,

   Baidu directly on the name of the download, or downloaded from the corresponding official website: generally used in jackson, javapns, log4j three packages

    jackson-core-2.9.9.jar

  javapns-jdk16-2.4.0.jar

  apache-log4j-2.12.0.jar (this may be several log4j jar of)

3. Install dependent jar package

   mac under java package installation directory / Library / Java / Extensions /

   We download package on top of the jar top directory can be;

4. Test java program codes

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 {
        static void main public (String [] args) throws Exception { 

                System.out.println ( "Start ZSL ========== push messages"); 
                int = badge. 1; // value of the icon turns red 
                String sound = "default"; // ringtone 
                // to be pushed, mobile device token number 
                String deviceToken = "753c86b495613089f02dcd3f735f0ada9e2d40f84c0a6360802ea57e55f43b8x"; 
                // here to push a test message 
                String = the message "the message to the push ios the test device"; 

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

                // must use the Java p12 exported file, php, then is pem file 
                // Note that the certificate is the production environment or test environment
                String certificatePath = "./APNS_iOS_3.p12"; 
                password from // mac keychain, provided export certificate 
                String msgCertificatePassword = ". 1"; 

                Boolean = sendcount to true; 

                PushNotificationPayload new new payload PushNotificationPayload = (); 
                payload.addAlert (Message) ; // message content 
                payload.addBadge (badge); 
                payload.addCustomDictionary ( "uid", "haahi"); 
                payload.addCustomDictionary ( "of the type", 12); 
                payload.addCustomDictionary ( "title", "haahi"); 
                payload .addSound ( "default.caf");// tones 

                PushNotificationManager pushManager = new PushNotificationManager ();
                // true: corresponding to the production environment iOS push false: the corresponding test environment iOS push  
                pushManager.initializeConnection (new AppleNotificationServerBasicImpl (certificatePath, msgCertificatePassword , true));
                List <PushedNotification> = new new Notifications the ArrayList <PushedNotification> (); 
                // start push message 
                IF (sendcount) { 
                        Device Device BasicDevice new new = (); 
                        Device. setToken (deviceToken); 
                        PushedNotification Notification = pushManager.sendNotification (Device, payload, to false); 
                        notifications.add (Notification); 
                } the else { 
                        List <Device> Devices new new = the 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 ( "message will push ZSL =========="); 
        } 
}

  

Code which has notes

Note that the need to push the phone above token, push path to the certificate, the certificate password push, push type production or test certificate;

 

5. Run the test

  Mac terminal at compile with javac

   javac PushMsg.java

  Then run the resulting PushMsg.class

   java PushMsg

  See logs, and mobile terminal receiving the notification to verify;

 

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==========消息推送完毕

  

     

Push Reference Code: https://www.jianshu.com/p/7a9f544a1ae3

 

Guess you like

Origin www.cnblogs.com/cocoajin/p/11250413.html