The principle of push: Push mechanism can be summarized as a simple diagram, provider refers to an iphone software Push server, this article I will use. NET as a provider. APNS is an abbreviation for Apple push Notification Service (Apple push server) and is Apple's server. Can be divided into three stages. First stage: The Push server application packages the message to be sent, the identity of the iphone, and sends it to APNS. Phase II: APNs in its own list of iphone's registered push services, look for an iphone with a corresponding logo and send the message to the iphone. The third stage: The iphone sends the message to the appropriate application and follows the settings to eject the push notification. From what we can see. 1, the first is the application registration message push. 2, iOS and APNs server to Devicetoken. The application accepts Devicetoken. 3. The application sends the Devicetoken to the push service-side program. 4. The service-side program sends messages to the APNS service. 5. The APNs service sends the message to the iphone app. Both the iphone client and APNs, provider and APNs need to be connected through a certificate. The following describes the production of the certificate used. I. CSR documents
1. Generate Certificate Signing Request (CSR) 2, fill in your mailbox and common name, and select Save to hard drive. Click Continue: This generates a pushtest.certsigningrequest file locally.
Second, SSL certificate file
1. Log in to iOS Provisioning Portal with your paid account number and create certificates (created to omit), such as: Click Submit to create the certificate complete. 2, create a new app ID click on the new app ID input description,bundle Identifier, click Submit, the new app ID is complete. Find the new app ID click on the configure:development push SSL Certificate on the right, the difference between production push SSL Certificate is a push certificate for development, one for publishing products of the push certificate. Two certificates get to the terminal Devicetoken is not the same, with two certificate generated P12 certificate for Java Background Connection APNs server address is also different, development Push SSL Certificate The server address for the corresponding connection is: gateway.sandbox.push.apple.com. Production Push SSL Certificate The server address of the corresponding connection is: gateway.push.apple.com. Click on the development Push SSL certificate Line after configure: Click Continue: Select the previous generated pushtest.certsigningrequest file, click Generate, the following page appears: Click Continue: Click Download, download the generated support for the push service certificate (named: Aps_ Development-6.cer).
Click Done and you will find that the status becomes enabled: So far, we have generated two files: 1, pushtest.certsigningrequest2, aps_development-6.cer (download the generated certificate that supports the push service. Double-click Aps_development-6.cer to register in your keychain, so you have three in your keychain, prepare the profile certificate, because the push message can only be tested on the real machine, so build a profile certificate click on "New Profile" To create a profile for the new app ID above, download pushtestdescdevprofile.mobileprovision and double-click it to add it to Xcode's provisioning Profiles. Iv. generate a Java backend certificate for connecting APNs: Open Keychain Select Apple development IOS Push Services:com.easecom.zhwgpushtestdesc and right-click to export it. Export the P12 certificate for the Java background connection APNs. Enter the password for the P12 certificate, which I used in this article is 123456. Remember this password, the Java background uses the P12 certificate to use. Enter the password to access the keychain: System login password. The Export PUSHTEST.P12 certificate is complete. So far, we have generated four files: 1, pushtest.certsigningrequest2, aps_development-6.cer (download the generated certificate that supports the push service. ) 3, Pushtestdescdevprofile.mobileprovision
4, PUSHTEST.P12 This iOS message push (Java background) certificate is all finished. The following code: Five, iOS code: 1, first in the project's APPDELEGATE.M to add the following two proxy methods
-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken { NSString *token = [NSString stringwithformat:@ "%@", Devicetoken]; To obtain the terminal equipment identification, this identity needs to be sent through the interface to the server side, server-side push message to APNs need to know the identity of the terminal, APNs through the registered terminal identity to find terminal equipment. NSLog (@ "My token is:%@", token); } -(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error { NSString *error_str = [NSString stringWithFormat: @ "%@", error]; NSLog (@ "Failed to get token, error:%@", error_str); }
2, in APPDELEGATE.M (BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (NSDictionary *) The Launchoptions method joins the registration message notification push ability; joins when the application is in an inactive state, determines whether it is triggered by a remote message notification, and joins the purge message push notification token.
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ Determines whether the application launches if triggered by a remote message notification ([launchoptions objectforkey:uiapplicationlaunchoptionsremotenotificationkey]!=nil) {/ /Gets the number of application message notification tokens (that is, the number in the small red circle) int badge = [UIApplication sharedapplication].applicationiconbadgenumber; if (badge>0) {//If the number of application message notification tokens (that is, the number in the small red circle) is greater than 0, clear the mark. badge--; Clears the tag. Clear the small red circle in the number, the small red circle in the number of 0, the small red circle will be eliminated. [UIApplication sharedapplication].applicationiconbadgenumber = badge; }}//message push registration [[UIApplication sharedapplication] registerforremotenotificationtypes:uiremotenotificationtypesound| Uiremotenotificationtypealert| Uiremotenotificationtypebadge];} 3, add the message Receive processing agent method in the project APPDELEGATE.M. Processing received message push-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo{ The received message is processed here. NSLog (@ "Receive Remote notification:%@", UserInfo);}
Vi. Java Backend Code:
public static void Main (string[] args) throws Exception {try {//Devicetoken obtained from client, in order to test simple, write fixed Identification of a test device. String Devicetoken = "Df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 440d34ad 6505f8c4" System.out.print ln ("Push Start devicetoken:" + Devicetoken); Define message pattern PayLoad PayLoad = new PayLoad (); Payload.addalert ("This is test!"); Payload.addbadge (1);//The number of message push marks, the number shown in the small red circle. Payload.addsound ("Default"); Registered Devicetoken Pushnotificationmanager Pushmanager = Pushnotificationmanager.getinstance (); Pushmanager.adddevice ("IPhone", Devicetoken); Connect APNs String host = "gateway.sandbox.push.apple.com"; String host = "gateway.push.apple.com"; int port = 2195; String Certificatepath = "C:/PUSHTEST.P12";//the *.p12 file location generated earlier for the Java Background Connection APNs service string certificatepassword = "1234 ";//p12 file password. Pushmanager.initializeconnection (host, Port, Certificatepath, Certificatepassword, sslconnectionhelper.keystore_ TYPE_PKCS12); Send push Device client = Pushmanager.getdevice ("IPhone"); SYSTEM.OUT.PRINTLN ("Push message:" + client.gettoken () + "\ n" +payload.tostring () + ""); Pushmanager.sendnotification (client, payLoad); Stop Connection APNs pushmanager.stopconnection (); Delete Devicetoken pushmanager.removedevice ("IPhone"); System.out.println ("Push End"); } catch (Exception ex) {ex.printstacktrace (); }}}The test passed. Above in Iphone4,ipad2 device test pass, iphone3g,3gs need to play pushdoctor (push doctor) patch to test pass.
Ext: IOS Based on APNs message push principle and implementation (Java background)