IOS development-Stage 4-Implementation of the push server, and stage 4 of ios
After writing push notification to get device token and get device token, you need to send the token string to the application server, that is, the provider.
The provider sends the token number, notification content, and notification form (for example, whether a prompt window is displayed or whether a voice is voiced) to the Apple Server (apns ).
The simplest provider implementation is to establish a secure connection (tsl or ssl) with the Apple Server through a certificate, and send data streams that meet Apple's requirements to the Apple Server after a connection is established through authentication.
Obtain certificate
Apple provides two access methods:
- Developer for testing
- Production, used for products
For internal testing, use developer.
Download the certificate through the ios provisioning portal:
This requirement:
- The log-on apple developer program account must be the highest-level agent (for enterprise accounts, it doesn't matter if it is a personal account). The agent account is the initial account; otherwise, the configure link cannot be seen;
- The developer and product must be enabled after the configure operation.
Go to the configure link and click download:
Process Certificate
If you are writing an objc program running on a mac, skip this step without processing the certificate.
If it is used in java, You need to combine and export the private key used for the certificate and the certificate supporting the notification (note, not the iphone developer Certificate) mentioned above.
Generate a certificate:
When you click storage, a file Password is prompted:
Of course, the password can be blank.
The following message is displayed:
Enter the password of the mac logon user.
File generation.
Write an instance for sending a notification
For mac code writing, there is a ready-made project available: http://stefan.hafeneger.name/download/PushMeBabySource.zip
To import to xcode, you only need:
Set deviceToken to the token string of the device. In addition, change pathForResource:
Aps_pai_identity
In addition, copy the certificate downloaded in the obtained certificate step to the xcode project Resources directory:
We can see that the file name is consistent with the above pathForResource parameter.
Then run the program to receive the push notification on the device.
If it is written in java, you can use a third-party library, see:
Http://code.google.com/p/javapns/
Write a simple notification sending code:
Import org. json. JSONException;
Import javapns. back. PushNotificationManager;
Import javapns. back. SSLConnectionHelper;
Import javapns. data. Device;
Import javapns. data. PayLoad;
Public class Main {
/**
* @ Param args
* @ Throws Exception
*/
Public static void main (String [] args) throws Exception {
PayLoad simplePayLoad = new PayLoad ();
// Get PushNotification Instance
PushNotificationManager pushManager = PushNotificationManager. getInstance ();
// Link the iPhone's UDID (64-char device token) to a stringName
PushManager. addDevice ("iPhone", "00000000 00000000 00000000 00000000 00000000 00000000 00000000 ");
SimplePayLoad. addAlert ("My alert message test ");
SimplePayLoad. addBadge (1 );
SimplePayLoad. addSound ("default ");
Device client = PushNotificationManager. getInstance (). getDevice ("iPhone ");
PushNotificationManager. getInstance (). initializeConnection ("gateway.sandbox.push.apple.com", 2195, "/home/ubuntu/mypush. p12", "password", SSLConnectionHelper. KEYSTORE_TYPE_PKCS12 );
PushNotificationManager. getInstance (). sendNotification (client, simplePayLoad );
No Chinese characters in the test are garbled.
Compile a complex example (you can control whether a notification has a prompt window or a reminder sound ):
- APayload. addBadge (2), number displayed on the mobile app icon
- APayload. addAlert ("software version updated"). The prompt text is displayed.
- APayload. addSound ("default.wav"), which specifies the prompt sound.
You can also use a third-party php implementation, for example:
Http://code.google.com/p/php-apns