The working mechanism of iOS message push can be simply summed up:
Provider refers to the push server for an iphone software, APNs is the Apple push Notification service abbreviation, is a server.
Can be divided into three stages:
Phase one: The 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 identity 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. Application registration message push.
2. iOS obtains device token from APNs server and the application receives device token.
3. The application sends device token to the push server 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 via a certificate.
Let me introduce some of the certificates 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 Push.certsigningrequest file locally.
Ii.. P12 Documents
1. Export the key.
2. Enter your password.
This generates a PUSH.P12 file.
Third, SSL certificate file
1. Log in to iOS Provisioning Portal with the account you paid for, and create a new app ID, which can be referenced in the iOS app's real-computer debugging, which results in the following record:
2. Click on the Configure on the right:
3, click Development Push SSL Certificate line after the Configure:
4. Click Continue:
5, select the previous generated push.certsigningrequest file, click Generate, appear the following page:
6. Click Continue:
7. Click Download, and name the file aps_developer_identity.cer.
8. Click Done and you will notice that the status becomes enabled:
Note: The Apple Push Notification Service column for some app IDs is grayed out and the Configure button is not allowed because APNs does not support app IDs with wildcard characters.
So far, we have generated three files:
1, Push.certsigningrequest
2, PUSH.P12
3, Aps_developer_identity.cer
Add the following code to the Didfinishlaunchingwithoptions method in the appdelegate of the project:
iOS code
- [[UIApplication Sharedapplication] registerforremotenotificationtypes: (Uiremotenotificationtypealert | Uiremotenotificationtypesound | Uiremotenotificationtypebadge)];
The Registerforremotenotificationtypes method tells the application that it can accept push notifications.
Add the following method to get Devicetoken in the appdelegate of the project:
iOS code
- -(void) Application: (UIApplication *) app Didregisterforremotenotificationswithdevicetoken: (NSData *) DeviceToken {
- nsstring *token = [nsstring stringwithformat:@ "%@", devicetoken];
- NSLog (@"My token is:%@", token);
- }
- -(void) Application: (UIApplication *) app Didfailtoregisterforremotenotificationswitherror: (Nserror *) Error {
- nsstring *error_str = [nsstring stringwithformat: @ "%@", error];
- NSLog (@"Failed to get token, error:%@", error_str);
- }
Get to the Devicetoken, we can submit to the background application, send the notification of the background application in addition to need to know Devicetoken, but also need a certificate with APNs connection.
This certificate can be obtained from the two files we generated earlier.
1. Convert Aps_developer_identity.cer to APS_DEVELOPER_IDENTITY.PEM format
Shell Code
- OpenSSL x509-in aps_developer_identity.cer-inform der-out aps_developer_identity.pem-outform PEM
2. Convert the private key in the P12 format to PEM
Shell Code
- OpenSSL pkcs12-nocerts-out push_noenc.pem-in PUSH.P12
3. Create a P12 file
Shell Code
- OpenSSL pkcs12-export-in aps_developer_identity.pem-inkey push_noenc.pem-certfile push.certsigningrequest-name "Aps_developer_identity"-out aps_developer_identity.p12
This way we get a certificate file for use in a background application such as. NET or Java: APS_DEVELOPER_IDENTITY.P12
If the background application is PHP, then you can follow the IOS message push mechanism in the generation of PEM files in this article to generate the certificate file used in the PHP background application: CK.PEM
Implementation of "ios" iOS messaging push mechanism