Preparation work before to prepare three documents: one is the CSR request file (CertificateSigningRequest), one is aps_development cer SSL certificate file, is there a push PushDeveloper. P12 secret key file. As shown below:

1. The step of generating the Certificate Signing Request is omitted.

2. Download the development certificate and release certificate

Go to the Description of Apple development website to download the certificate, take the development certificate as an example. As shown below:

3. Export the key from keystring access

Open the keystring access and find our private key (the private key is the local private key that we downloaded and installed the push certificate), as shown below:

When exporting the. P12 file, you need to enter the password. I choose 123456 as the simple password, which needs to be remembered for later use.

After the preceding steps are complete, open the terminal CD and go to the p12 directory, where the three files are stored

1. Convert the SSL certificate of aps_development.cer to pushchatcert. pem and run the following command:

openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
Copy the code

A pushchatcert. pem file is generated in the P12 directory

2. Set the password abc123 to convert the private key in pushdeveloper. p12 format to pushchatkey. pem

openssl pkcs12 -nocerts -out PushChatKey.pem -in PushDeveloper.p12
Copy the code

3. Use certificate and pushchatkey. pem to create the apns_developer_identity.p12 file (used for Java background configuration), and enter the secret language abc123

openssl pkcs12 -export -in PushChatCert.pem -inkey PushChatKey.pem -certfile CertificateSigningRequest.certSigningRequest -name "apns_developer_identity" -out apns_developer_identity.p12
Copy the code

Ios Engineering Testing

Write in the AppDelegate didFinishLaunchingWithOptions function

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if ([[[UIDevice currentDevice] systemVersion] floatThe Value] > = 8.0) {[[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; }else{/ / here is the original code [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }return YES;
}
 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
      NSLog(@"regisger success:%@", [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">"withString:@""]stringByReplacingOccurrencesOfString:@""withString:@""]); } / / receptionist received messages - (void) application: (UIApplication *) application didReceiveRemoteNotification: (the userInfo NSDictionary *) {/ / Handle push messages NSLog(@"userinfo:%@",userInfo);
    
    NSLog(@"Received push message :%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Registfail%@",error);
    NSLog(@"DeviceToken failed to obtain because: %@",error);
}
Copy the code

Java background code

public static void main(String[] args) throws Exception{ int badge = 1; // String sound ="default"; // Ring tone String msgCertificatePassword ="123456"; / / export certificates set password / / 90 fb73e94659a1822caa51ca079734de6a7e60e44f260a1bfd1326bb4648d734 String deviceToken ="a1f52f971ca720d6ffc98ce7212c74edf347234ab2cc34fefcb541314e2e13e1"; // Mobile device token String message ="test push message to ios device11111111111"; List<String> tokens = new ArrayList<String>(); tokens.add(deviceToken); // Java must export p12 files using the PEM file String certificatePath = in PHP"/Users/xxxx/Desktop/p12/apns_developer_identity.p12";
        boolean sendCount = true; PushNotificationPayload payload = new PushNotificationPayload(); payload.addAlert(message); // Message content payload. AddBadge (badge); //payload.addCustomAlertBody(msgEX);if (null == sound || "".equals(sound)) {
            payload.addSound(sound);
        }

        PushNotificationManager pushManager = new PushNotificationManager();
        // false: indicates the product test push servicetrue: Represents the product release notification service pushManager. InitializeConnection (new AppleNotificationServerBasicImpl (certificatePath, msgCertificatePassword,false)); List<PushedNotification> notifications = new ArrayList<PushedNotification>(); // Start pushing messagesif (sendCount) {
            Device device = new BasicDevice();
            device.setToken(deviceToken);
            PushedNotification notification = pushManager.sendNotification(
                    device, payload, true);
            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 ========== Success number:" + successful);
        System.out.println("ZSL ========== number of failures:" + failed);
        pushManager.stopConnection();
        System.out.println("ZSL ========== message push completed");
    }
Copy the code

Add the following dependencies to your POM.xml:

< the dependency > < groupId > com. Making. Fernandospr < / groupId > < artifactId > javapns - jdk16 < / artifactId > < version > 2.3.1 < / version > </dependency> <dependency> <groupId>log4j</groupId>
           <artifactId>log4 j < / artifactId > < version > 1.2.17 < / version > < / dependency >Copy the code