From: http://www.cocoachina.com/bbs/read.php? Tid = 30410
BeginnerIPhoneDevelopment, After repeated verification, combined with the following two Tutorials:
Http://ameyashetti.wordpress.com/2009/07/31/apple-push-notification-service-tutorial/
Http://www.macoscoders.com/2009/05/17/iphone-apple-push-notification-service-apns/
The following figure shows the PHP version push server construction process starting from scratch:
========================================================== ======================================
0. install on Mac OS XXcodeConnect to a normal iPhone to maintain a peaceful attitude
app development basic Settings
1. create the app ID and device in the iPhone provisioning portal.
2. in keychain access. the application generates a certificate request certificatesigningrequest. certsigningrequest (menu> keychain access> certificate assistant> request a certificate from a certificate authority ...).
3. in iPhone provisioning portal> certificates, request a certificate (Click request certificate to upload certificatesigningrequest. certsigningrequest ).
4. after the request is complete, the certificate download the file (pai_identity.cer) and double-click it to import it to the key chain.
5. on the iPhone provisioning portal> provisioning, create a profile, select the specified app ID and devices, and generate the profile.
6. download the generated profile as * _ profile. mobileprovision: double-click the file to load the profile to the iPhone.
Push notification service settings
7. On the iPhone provisioning portal> app IDs, select the app ID of the push service to go to configure.
8. Confirm enableApplePush notification service, configure development push SSL certificate,UploadThe certificate request generated in step 1.
9. Download the generated aps_developer_identity.cer to complete the push service configuration.
10. Double-click aps_developer_identity.cer and save it to the key chain.
Generate the Certificate file required by PHP push notification sender
11. In the keychain access. app, select the new certificate (Apple development push services *), export it to the desktop, and save it as certificates. p12.
12. Run the following command:
|
Copy code
- OpenSSL PKCS12-clcerts-nokeys-out cert. pem-in certificates. p12
- OpenSSL PKCS12-nocerts-out key. pem-in certificates. p12
- OpenSSL RSA-in key. pem-out key. unencrypted. pem
- Cat cert. pem key. unencrypted. pem> CK. pem
|
Get the device token required for PHP push notification Sender:
13. Create a view-based application project in $ project_nameappdelegate.m:
A. paste the followingCode:
|
Copy code
- -(Void) applicationdidfinishlaunching :( uiapplication *) app {
- // Other setup tasks here ....
- [Window addsubview: viewcontroller. View];
- [Self alertnotice: @ "" withmsg: @ "Initiating remote noticationss are active" canclebuttontitle: @ "OK" otherbuttontitle: @ ""];
- [[Uiapplication sharedapplication] registerforremotenotificationtypes :( uiremotenotificationtypealert | uiremotenotiftypetypebadge | uiremotenotiftypetypesound)];
- }
- -(Void) Application :( uiapplication *) app didregisterforremotenotificationswithdevicetoken :( nsdata *) devicetoken {
- // Nslog (@ "devtoken = % @", devicetoken );
- [Self alertnotice: @ "" withmsg: [nsstring stringwithformat: @ "devtoken = % @", devicetoken] canclebuttontitle: @ "OK" otherbuttontitle: @ ""];
- }
- -(Void) Application :( uiapplication *) app didfailtoregisterforremotenotificationswitherror :( nserror *) Err {
- Nslog (@ "error in registration. Error: % @", err );
- [Self alertnotice: @ "" withmsg: [nsstring stringwithformat: @ "error in registration. Error: % @", err] canclebuttontitle: @ "OK" otherbuttontitle: @ ""];
- }
- -(Void) alertnotice :( nsstring *) Title withmsg :( nsstring *) MSG canclebuttontitle :( nsstring *) cancletitle otherbuttontitle :( nsstring *) othertitle {
- Uialertview * alert;
- If ([othertitle isequaltostring: @ ""])
- Alert = [[uialertview alloc] initwithtitle: Title message: MSG delegate: Self cancelbuttontitle: cancletitle otherbuttontitles: nil, nil];
- Else
- Alert = [[uialertview alloc] initwithtitle: Title message: MSG delegate: Self cancelbuttontitle: cancletitle otherbuttontitles: othertitle, nil];
- [Alert show];
- [Alert release];
- }
|
B. Add in-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {Method
|
Copy code
- [Self alertnotice: @ "" withmsg: @ "Initiating remote noticationss are active" canclebuttontitle: @ "OK" otherbuttontitle: @ ""];
- [[Uiapplication sharedapplication] registerforremotenotificationtypes :( uiremotenotificationtypealert | uiremotenotiftypetypebadge | uiremotenotiftypetypesound)];
|
14. Project Settings
A. Targets> $ app_name> context menu> Properties> identifier
Change identifier to app ID
B. Targets> $ app_name> context menu> build> code signing identifier> any iPhone OS device
Specify the iPhone developer as the development Machine
15. After compilation and running, the device token will be displayed on the iPhone
16. php push notification sender code:
|
copy the Code
- $ devicetoken = "device token";
- $ body = array ("APs" => array ("alert" => 'message', "badge" => 1, "sound" => 'received5. CAF ');
- $ CTX = stream_context_create ();
- stream_context_set_option ($ CTX, "SSL", "local_cert", "Ck. pem");
- $ fp = stream_socket_client ("SSL: // gateway.sandbox.push.apple.com: 2195", $ err, $ errstr, 60, stream_client_connect, $ CTX);
- If (! $ FP) {
- Print "failed to connect $ err $ errstrn";
- return;
- }
- Print "connection OK \ n";
- $ payload = json_encode ($ body);
- $ MSG = CHR (0 ). pack ("N", 32 ). pack ("H *", $ devicetoken ). pack ("N", strlen ($ payload )). $ payload;
- Print "sending message:". $ payload. "\ n";
- fwrite ($ FP, $ MSG);
- fclose ($ FP);
- ?>
|
=========================================== ======================================