Combine all the information on the Internet, and then simply organize a copy.
First, APNs push instructions
1. Your iOS app needs to register for the APNS message push feature.
2. When the Apple APNs push receives the registration message from your app, it will return a bunch of device tokens to you (very important)
3. Pass the device token received by the app to your local push server.
4. When you need to push a message for the app, your local push server will send the message, along with the device token, to Apple's APNs service.
5.APNS then pushes the message to the destination iphone
Ii. preparatory work for the push
The main push preparation is 1, push certificate 2, the exported key file 3, download the description file
1. APP ID Creation
First create the app ID, named Pusttest, Bundle ID is com. Company name. Testpush
Click Next directly to complete the creation.
2. Generate certificate
Certificate Select push Push certificate, next select the app ID you just created
To add a request file exported from a keychain
Next, download the push certificate.
3. description file Creation Download
Profile selection Development, if you choose to select the following production, the project may be rolled back. The next step is to select the app ID you just created, and then select the device step-by-step and finally download the profile.
4. Key Export
Double-click the downloaded certificate file, will be in the keychain login-my certificate to generate a new certificate, right-click the export certificate, the name is set to PUSH.P12
Export the password to remember, push to use, I set a bit aaa123;
5, finishing
Put the key, certificate, and description file in a folder on the desktop, the folder name is set to "push";
6, File conversion PEM
Push the prepared files ready, and then start pushing to convert the keys and certificates into PEM files. The terminal command is required to generate a PEM file by opening the terminal in your computer.
First use the terminal command to open the desktop's push folder " CD ~/desktop/push " carriage return;
Generate a PEM file command for the certificate file " OpenSSL x509-in aps_development.cer-inform der-out pushcert.pem "
The key P12 file generates a PEM file command " OpenSSL pkcs12-nocerts-out pushkey.pem-in push.p12 ", enter the password three times, that is, the password to export time aaa123
Then merge the two PEM files into a PEM file and command: " cat Pushcert.pem pushkey.pem > Ck.pem " so that the push folder on the desktop has 6 files.
Test whether the push certificate is working correctly, enter the terminal command "telnet gateway.sandbox.push.apple.com 2195", and if it runs as follows, everything is fine.
Connect Apple server APNs, Terminal command " OpenSSL s_client-connect Gateway.sandbox.push.apple.com:2195-cert pushcert.pem-key Pushkey.pem "
Enter the password aaa123, return a bunch of data, and then the following. That means no problem.
7. Project configuration
New project, Bundle ID set as in app ID testpush,code signing Select profile and Development Account,
APPDELEGATE.M Set up registration,
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {if([Application respondstoselector: @selector (isregisteredforremotenotifications)]) {//IOS8//Create a uiusernotificationsettings and set the display class type of the messageUiusernotificationsettings *notisettings = [uiusernotificationsettings settingsfortypes: ( Uiusernotificationtypebadge | Uiusernotificationtypealert |uiremotenotificationtypesound) Categories:nil]; [Application registerusernotificationsettings:notisettings]; [Application registerforremotenotifications]; } Else{//iOS7[Application registerforremotenotificationtypes: (Uiremotenotificationtypebadge | Uiremotenotificationtypesound |Uiremotenotificationtypealert)]; } Self.window=[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; Self.window.backgroundColor=[Uicolor Whitecolor]; [Self.window makekeyandvisible]; //Self.mainview = [[Viewcontroller alloc] initwithnibname:@ "Mainviewcontroller" bundle:nil];Self.window.rootViewController =[Viewcontroller alloc]; returnYES;}
Other callback methods
//Handling Push Messages- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) userinfo{NSLog (@"userinfo:%@", UserInfo); NSLog (@"receive push message:%@", [[UserInfo Objectforkey:@"APS"] Objectforkey:@"Alert"]);}//Registration failed callback- (void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error{NSLog (@"registfail%@", error);}//return Device token-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) devicetoken{NSLog (@"Equipment Marking%@", Devicetoken);//the token here is the token that our device will tell the server.}
After running get labeled Device token "F465312AB96E2DFD35829D1C6F898A019B01141615745F5B41A4DB4A312870A0"
8. PHP Local Server
The test notification effect requires a PHP file, which fills in the obtained flag, the key export password, and the merge file Ck.pem
<?PHP$deviceToken= ' f465312ab96e2dfd35829d1c6f898a019b01141615745f5b41a4db4a312870a0 ';//gets the Devicetoken in the request parameter $passphrase= ' aaa123 ';//mimi$message= ' This is a push message ';//get the information you want to push in the request parameters///Print View Echo $deviceToken; Echo $message; //Put your alert message here: Set up a certificate and password that you have applied and converted well $ctx=stream_context_create(); stream_context_set_option($ctx, ' SSL ', ' Local_cert ', ' Ck.pem ');//ck.pemstream_context_set_option($ctx, ' SSL ', ' passphrase ',$passphrase); //Open A connection to the APNS server//$fp = stream_socket_client (' ssl://gateway.push.apple.com:2195 ', $err, $e Rrstr, Stream_client_connect, $ctx); $fp=stream_socket_client(' ssl://gateway.sandbox.push.apple.com:2195 ',$err,$errstr, stream_client_connect|. Stream_client_persistent,$ctx); if(!$fp) Exit("Failed to connect:$err $errstr".Php_eol); Echo' Connected to APNS '.Php_eol; //Create the payload body $body[' APS '] =Array(' alert ' =$message, ' sound ' = ' default '); //Encode the payload as JSON $payload= Json_encode ($body); //Build the binary notification $msg=CHR(0).Pack(' n ', 32).Pack(' h* ',$deviceToken) .Pack(' n ',strlen($payload)) .$payload; //Send It to the server $result=fwrite($fp,$msg,strlen($msg)); if(!$result) Echo' Message not delivered '.Php_eol; Else Echo' Message successfully delivered '.Php_eol; //Close The connection to the server fclose($fp); ?>
php file Download:http://pan.baidu.com/s/1bi3Lym
9. Push command
Put the configured PHP files in the Push folder on the desktop as well. Run the terminal command to send a notification to the labeled phone "This is a push message"
Use the terminal command CK to open the Push folder, and then call the run push.php file, run the command: " php push.php"
The phone receives a notification message.
Note: The main content of this article is to refer to other online blog content, a little collation.
IOS Notification Push APNs