Don't say anything else, now add the following code block to the APPDELEGATE.M
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{// Insert this line of code into [[UIApplication sharedapplication] registerforremotenotificationtypes:uiremotenotificationtypebadge| Uiremotenotificationtypesound];}
-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) devicetoken{NSLog (@ "Devicetoken =%@", Devicetoken);}
-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error {NSLog (@ "Error in registration. Error:%@ ", error);}
-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo{ Nsdictionary *dic = [UserInfo objectforkey:@ "APS"]; NSLog (@ "received push message%@", [dic objectforkey:@ "alert"]); if (nil! = [dic objectforkey:@ "alert"]) {Uialertview *alterview = [[Uialertview alloc] initwithtitle:@ "Push notification" Messa Ge:[dic objectforkey:@ "alert"] delegate:self cancelbuttontitle:@ "Off" otherbuttontitles:@ "update Status", nil]; [Alterview show]; [Alterview release]; }}
Remove the blanks and <> symbols from the printed devicetoken, and generate a license number to keep
======================================================
To generate a test certificate:
1, enter provisioning Portal, download certificates certificate under development
2. Locate the app ID that you want to test, and then enable it under development Apple push Notification service:development push SSL Certificate. You need to enter the signing certificate in 1 to generate a aps_developer_identity.cer.
3, double-click Aps_developer_identity.cer, will open the System key chain. Find Apple Development Push Services under my certificates. You need to export a. p12 file for certificate and the private key under it (the arrow at the front of the point, which will appear as a key). (The setup password process will appear)
4. Convert the above 2. p12 files to. pem format:
OpenSSL pkcs12-clcerts-nokeys-out cert.pem-in CERT.P12
OpenSSL pkcs12-nocerts-out key.pem-in KEY.P12
5. If you need to do not encrypt key:
OpenSSL rsa-in key.pem-out Key.unencrypted.pem
6. Combine two. Pem files, this CK.PEM is the certificate required by the server.
Cat Cert.pem Key.unencrypted.pem > Ck.pem
================================================================
Write Server send.php with PHP
#!/usr/bin/php<?php$devicetoken = ' 3cec682c9b773f2d82e337a98c35483aaa4f4c74e3933f9cb57e9d4c2f17f52d '; // masked for security reason// Passphrase for the private key (ck.pem file) $pass = ';// get the parameters from http get or from command line$message = $_get[' message '] or $message = $argv [1] or $ message = ' message received from javacom '; $badge = (int) $_get[' badge ' or $badge = (int) $argv [2]; $sound = $_get[' sound '] or $sound = $ARGV [3]; $message = ' hello qwe '; $badge = 1; $sound = ' cowbell.wav ';// construct the notification payload$body = array (); $body [' APs '] = Array (' alert ' => $message);if ($badge) $body [' APs '] [' badge '] = $badge; if , ($sound) $body [' APs '] [' sound '] = $sound;/* end of configurable items */ $ctx = stream_context_create (); Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', ' Ck.pem ');// assume the private key passphase was removed.// stream_ Context_set_option ($ctx, ' SSL ', ' passphrase ', $pass); $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;} else {print "connection ok\n";} $payload = json_encode ($body); $msg = chr (0) . pack ("n", +) . pack (' h* ') , str_replace (' ', ', $deviceToken)) . pack ("N", strlen ($payload)) . $ payload;print "SENDING&NBSP;MESSAGE&NBSP;:" . $payload . "\ n"; fwrITE ($fp, $msg); fclose ($FP);? >
Put the PHP file in the same folder as the CK.PEM.
From the command line, PHP send.php can be tested