【IMessage real machine script source code mass distribution】¶
【IMessage real machine script source code mass distribution】Does pushing content require a public key certificate to operate?¶
Apple Push Notification Service is a message push service provided by Apple. The push mode includes top message bar, sound and badge number(). With APNS, the application can receive program-related information in any situation (including running status not running, foreground and background), because in most cases, iOS At least one application can be in the active state, so APNS provides great convenience for application interaction. 1: Introduction Before using APNS, there are a few things you need to understand: 1: APNS is charged. Only with a developer account can you apply for an APNS certificate. 2: APNS is unreliable. Apple does not guarantee the reliability of information push. 3: APNS has no limit on the size of the message, and the total capacity cannot exceed 256 bytes. After understanding the above three items, you should have an understanding of the applicable application scenarios of APNS.
The workflow of APNS is shown in the figure below: 1 & 2: When the user installs and starts the application for the first time, a dialog box will pop up to remind the application that it needs to be pushed openly and whether it is allowed. If it is allowed, the application will obtain a hardware token. There are three points to note: First, this token is uniquely related to the device, and the token obtained by different applications on the same device is the same; Second, when the application is uninstalled and then reinstalled, the confirmation dialog box will not appear again, and the configuration information of the previous installation will be automatically inherited; Third, push settings can be changed in Settings-Control. You can choose to turn on one or more of message box, sound and badge number. 3: The application sends the received token to the server, which is the source of the APNS message. 4: The application server sends a message to Apple’s message server through the token and certificate. 5: Apple sends the received message to the corresponding application on the corresponding device. 6: If the application is not in the Active state (not started or backgroud), under the default settings, a message box will pop up at the top of the screen with a sound prompt. Click to change the message box to enter the application. If you do not click, there will be a badge number on the application icon. Appear. Two: Use pace The use of APNS is not complicated, but there are many links that are easy to make mistakes, especially the certificate application part, so special attention should be paid. The following is a discussion based on the actual steps I followed in the tutorial: Preparatory work: A: An Xcode project, we named it MyPushChat, and a corresponding App ID. B: An iOS device that can be used for debugging (APNS can only work on physical devices, and the simulator cannot run) step1: Open "Keychain Access" in "Applications - Tools", as shown in the figure below: In the next dialog box, when you choose to save to disk, you can fill in the email as you like and name it MyPushChat. Click "Inherit", set the file name to "MyPushChat", and click Save. In this way, you will get a file named "MyPushChat.certSigningRequest", which should be kept properly. Get the private key from the csr file you just created. The detailed operation is as shown in the figure below: Name the exported file MyPushChatKey.p12 and enter the password. Please remember this password. It is temporarily set to 123456abc. At this point, we have the file MyPushChat.certSigningRequest, and MyPushChatKey.p12 step2: Find the AppID corresponding to MyPushChat in App IDs, click the "Configure" button on the right, and check the selection box as shown below: Click the configure button on the right side of "Development Push SSL Certificate". The development version is used for testing. It is only valid for one year and can only use Apple's APNS test server. When the application is released, you need to apply for the Distributions version certificate. The Tokens obtained by the Development and Distribution version certificates are different. The pop-up box is as follows: Upload "MyPushChat.certSigningRequest" and click Generate. After a while, the certificate is generated. Download it and name it "aps_developer_identity.cer". step3: Open the Provision Portal, click New Provision, name the Provision File "MyPushChat", select the corresponding App ID and Device and download it. Get the file MyPushChat.provision. Double-click to import this MyPushChat.Provision file. If everything is normal, Orgnizer will pop up, and the display interface will be as follows: step4: Save all the files obtained above to the desktop. Open the Console and switch to the desktop. First convert aps_developer_identity.cer to MyPushChat.cert Command: openssl x509 -in aps_developer_identity.cer -inform der-out MyPushChatCert.pem Then convert the private key file to MyPushChatKey.pem Order: openssl pkcs12 -nocerts -out MyPushChatKey.pem -in MyPushChatKey.p12 Enter Import Password: The password entered here is the password set later for the private key: 123456abc
MAC verified OK
Enter PEM pass phrase:
You must enter a new password here, we set it to 123456abc
Verifying - Enter PEM pass phrase:
Next, split MyPushChatKey.pem and MyPushChatCert.pem into a pem file:
Command: cat PushChatCert.pem PushChatKey.pem > ck.pem
Finally, test the obtained ck.pem file
First run:
Command: telnet gateway.sandbox.push.apple.com 2195
If the network is normal, the following will appear, ctrl + C to stop the connection.
Trying 17.172.232.226...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.
Then test the connection using ssl
Command: openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert MyPushChatCert.pem -key MyPushChatKey.pem
After entering the password 123456abc, if everything is normal, there will be a lot of output, you will be able to enter as many characters as you want, and after pressing Enter, the connection will be interrupted.
At this point, the most tedious and error-prone process has been completed. The certificate-related work ends here and we enter the coding stage~
step5: 1:
Add the following code to didFinishLaunchingWithOptions in AppDelegate.m in the project MyPushChat [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificatinTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; The function of this code is to pop up a dialog box when the application is started for the first time to let the user confirm whether to turn on message push. The message types registered in this sentence include BadgeNumber, sound, and top message box. You can choose one or more of them. 2: Add the following code to AppDelegate
-
(void)application:(UIApplication)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData)deviceToken { NSLog(@"My token is: %@", deviceToken); }
-
(void)application:(UIApplication)application didFailToRegisterForRemoteNotificationsWithError:(NSError)error { NSLog(@"Failed to get token, error: %@", error); } If the token is obtained successfully, there will be output in the following format in the console after running: My token is:<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>
Save the contents of angle brackets for later use
Similarly, add the following code to AppDelegate
- (void) application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo { if (application.applicationState == UIApplicationStateActive) { // The program receives push notifications while it is running NSLog("%@", [[userInfo objectForKey: @"aps"] objectForKey: @"alert"]); } else { //The program receives push notifications when it is running } } The above code handles how the application receives push notifications in running and inactive states. 3: Download the PHP sample program and set the devicetoken field to the token you just saved. Note that spaces are removed. Set password to 123456abc, set message to what you want to set, save, then enter the php source code path from the command line and run php simplepush.php If your moral character is good enough, your device will immediately make a buzzing sound~ 3: Other things to note 1: You can use the following code to decisively turn on those types of message notifications: UIRemoteNotificationType enabledTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (enabledTypes & UIRemoteNotificationTypeBadge) { //Enable badge number } if (enabledTypes & UIRemoteNotificationTypeSound) { //turn on sound } if (enabledTypes & UIRemoteNotificationTypeAlert) { //turn on alert } 2: It is recommended to use Javapns for the push server. It is very simple to use. Please note that the certificate file used is not pem, but p12 format. The specific generation method is: 1: Generate csr file (same as above) 2: Generate cert file on Apple website through csr (same as above) Three: Double-click the generated cert file to import, select the public key of the csr and the ssl certificate just imported in the keychain, right-click -> export, save as p12 Other processes are similar 3: If you are confident, you can indirectly use the distribution version of the certificate and provision file, but the online server has certain restrictions. If used improperly, it will be banned by Apple. 4: Apple’s push server will return a sending result to the application server. For objects that continue to fail, the application server needs to handle them. 5: The passed message is in json format, and you can add your own fields to it, but again, the total size cannot exceed 256 bytes.