The working mechanism of IOS message push can be summarized as follows:
Provider refers to the push server of an iPhone software. apns is the abbreviation of Apple push notification service and is the server of apple.
There are three phases:
Phase 1: ApplicationProgramPackage the message to be sent and the ID of the target iPhone to apns.
Stage 2: apns searches for the iPhone with the corresponding identifier in its registered PUSH Service iPhone list and sends the message to the iPhone.
Stage 3: The iPhone transmits the sent message to the corresponding application, and a push notification is displayed according to the settings.
We can see that:
1. The application registers for message push.
2. iOS obtains the device token from the apns server, and the application receives the device token.
3. The application sends the device token to the push server program.
4. The server program sends a message to the apns service.
5. The apns Service sends messages to the iPhone application.
Both the iPhone client and apns, provider and apns, must be connected through a certificate.
Below I will introduce several certificates used.
1. CSR File
1. Generate a Certificate Signing Request (CSR)
2. Enter your email address and common name, and save it to the hard disk.
Click to continue:
In this way, a push. certsigningrequest file is generated in the response.
Ii. p12 files
1. Export the key.
2. enter your password.
In this way, a push. p12 file is generated.
Iii. SSL Certificate file
1. log on to the IOS provisioning portal with the account you paid, and create an app ID. For details about this process, refer to debugging the iOS app's real machine, this will generate the following record:
2. Click Configure on the right:
3. Click the configure next to the SSL Certificate row pushed by development:
4. Click "continue:
5. Select the previously generated push. certsigningrequest file and click Generate. The following page is displayed:
6. Click "continue:
7. Click Download and name the file aps_developer_identity.cer.
8. Click Done and you will find that the status has changed to Enabled:
Note: The Apple push notification service column of some app IDS is gray and the Configure button is not allowed because apns does not support app IDs with wildcards.
So far, we have generated three files:
1. Push. certsigningrequest
2. Push. p12
3. aps_developer_identity.cer
Add the followingCode:
IOS code
- [[Uiapplication sharedapplication] registerforremotenotificationtypes :( uiremotenotificationtypealert | uiremotenotiftypetypesound | uiremotenotiftypetypebadge)];
The registerforremotenotificationtypes method is used to notify the application to receive push notifications.
In the project's appdelegate, add the following method to obtain the devicetoken:
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 );
-
- }
We can submit the obtained devicetoken to the background application. The background application that sends the notification must know the devicetoken and a certificate connected to apns.
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 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_pai_identity"-Out aps_identity_p12
In this way, we get the certificate file used in background applications such as. Net or Java: aps_rj_identity.p12
If the background application is Php, you can generate this article according to the PEM file in the IOS message push mechanism.ArticleTo generate the Certificate file used in the PHP background application: CK. pem