The principle of push:
The working mechanism of Push can be summarized simply as
In the figure, provider is the push server for an iphone software, which I will use. NET as the 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 choose to save to the hard disk.
Click Continue:
This generates a Pushtest.certsigningrequest file locally.
Second, SSL certificate file
1. Log in to the iOS Provisioning Portal with your paid account number and create a certificates (can be omitted), such as:
Click Submit to create 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 on the right:
Development push SSL Certificate differs from production push SSL Certificate in that one is a push certificate for development and one is a push certificate for publishing a product. 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 previously generated pushtest.certsigningrequest file, click Generate, and the following page appears:
Click Continue:
Click Download to download the generated certificate for the supported push service (named: aps_development-6.cer).
Click Done and you will notice that the status becomes enabled:
So far, we have generated two files:
1, Pushtest.certsigningrequest
2. 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 your keychain will have
Third, prepare the profile certificate, because the push message can only be tested on the real machine, so to build a profile certificate Click "New Profile" to create a profile for the new app ID above, After successful download pushtestdescdevprofile.mobileprovision double-click to add it to Xcode's provisioning Profiles.
Iv. generate a Java backend certificate for connecting APNs:
Open the 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.certsigningrequest
2. Aps_development-6.cer (Download the generated certificate that supports the push service.) )
3, Pushtestdescdevprofile.mobileprovision 4, PUSHTEST.P12
This completes the IOS message push (Java background) certificate.
The following code begins:
Five, the iOS port code:
1, first in the project APPDELEGATE.M to add the following two agent 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 starts by a remote message notification trigger
if ([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 marker.
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 background code:
public static void Main (string[] args) throws exception { try { / /Devicetoken obtained from the client, in order to test simple, write fixed a test device identification. String devicetoken = "Df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 440d34ad 6505f8c4 "
System.out.println ("Push Start devicetoken:" + Devicetoken); //define Message Patterns 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"); //Registration Devicetoken Pushnotificationmanager Pushmanager = Pushnotificationmanager.getinstance (); pushmanager.adddevice ("IPhone", Devicetoken); //Connection APNs String host = " Gateway.sandbox.push.apple.com "; //string host = "gateway.push.apple.com"; int port = 2195;
String Certificatepath = "C:/PUSHTEST.P12";// Previously generated *.P12 file location for the Java Background Connection APNs service String Certificatepassword = "123456";//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.