Yesterday did a bit of remote push, write it down today and share it to the people who need it. Refer to a lot of articles, perhaps iOS8 changes, no one can be completely looked down, so I intend to write an article.
Backstage I also wrote, with the sae,php code, very simple, call the SAE package of a class can be sent to APNs to send information.
First, let's take a look at the Apple push mechanism. As the name implies, push refers to the server to send a message to the client, then in iOS, the application is suspended in the background, and can not always connect to the network, then how can the server send the message to the client? This is used to push, Apple's push mechanism, is only sent by APNs push notifications, so your own background server want to send the Client push notification, you have to push the content sent to APNs.
Speaking of which, if you do not know the Apple's push mechanism, you can view the data yourself. In short the overall process is: When the app starts, get the device_token of the device, and then put this device_token tell your own server, the server to get this device_token, use the certificate file, An SSL connection is initiated to Apple's APNs server, and a JSON string is sent after the connection is successful, and the JSON contains Device_token, notification content, and other things. After the Apple APNs gets the JSON, it finds the device push message via Device_token.
Well, our iOS front-end workload is very simple, just need to get device_token and be notified after doing what is possible. So, how to get the Device_token,ios8 of the device uses a new method. Called when the app is launched, such as in application:didfinishlaunchingwithoptions: This method, invokes the app's registered remote Push request method.
1 // iOS8 2 3 Uiusernotificationsettings * S =[uiusernotificationsettings settingsfortypes:uiusernotificationtypesound | Uiusernotificationtypealert | Uiusernotificationtypebadge Categories:nil]; 4 5 [[UIApplication sharedapplication] registerusernotificationsettings:s]; 6 7 [Application registerforremotenotifications];
If the registration is successful, a method will be callback, the code is as follows
1- (void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken2 {3NSString *dectoken = [NSString stringWithFormat:@"%@", Devicetoken];4 //Remove the angle brackets and the middle spaces after getting to5Nsmutablestring *st =[nsmutablestring Stringwithstring:dectoken];6[St Deletecharactersinrange:nsmakerange (0,1)];7[St Deletecharactersinrange:nsmakerange (st.length-1,1)];8NSString *string1 = [St Stringbyreplacingoccurrencesofstring:@" "Withstring:@""];9 //Save to localTenNsuserdefaults *u =[Nsuserdefaults standarduserdefaults]; One[U setobject:string1 forkey:@"Devicetoken"]; A}
Corresponding, there must also be a callback that failed to register. Write the error-handling code in it, here is not an example.
Have you ever thought that the normal push notifications will be in our notification panel, and if I am using this app, then the push will definitely not go to the notification panel, how do we find it? In the following callback method, when we are using the app, we receive a notification, then the app will call back the method without popping the notification.
1- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo2 {3NSLog (@"Receive Remote notification:%@", userInfo);4Uialertview *alert = [[Uialertview alloc] Initwithtitle:@"Tips"message:userinfo[@"APS"][@"Alert"]Delegate: Self Cancelbuttontitle:@"Good"Otherbuttontitles:nil, nil];5 [alert show];6 7}
In this method, we can handle the messages we receive.
The front-end task is so much, don't forget to hand over the Device_token to your server.
The service-side code was not written by us, but it was not written, so they went to see Apple's official document. If you don't understand it, look for a ready-made code to study it.
Program Ape Siege Lions are omnipotent, not only will repair the computer, but also to write backstage. So, let's see how the backstage is implemented.
First, following the process above, after the server gets to Device_token and uses the certificate to send a push request to APNs, the problem comes. Excavator technology which strong?
Where does the push certificate come from?
Need a developer account. Yes, like our real-machine debug certificates, at least 99 knives are required. With the account number, we can apply for a certificate. Below, followed by a step-by-step look, more trouble.
1. A certificatesigningrequest.certsigningrequest file needs to be generated on MacOS. Open keychain Access in the application, select Certificate Assistant from the menu-"Request a certificate from a certification authority"
Then save it locally on the line. This allows us to obtain a certificate request file.
2. The developer account will be used here. Enter developer.apple.com, choose numbercenter-"certificates, Identifiers & profiles-" identifiers-"APP IDs. Click the + sign in the upper right corner, register a AppID, write the ID name, and the default is explicit APP ID to write a bundle name. Also tick the push notifications under app services. This will be registered well.
3. Generate a push certificate. Click Certificates, upper right corner +, create a certificate, take development as an example, choose Apple Push Notification service SSL (Sandbox), and then go directly to the next, will let you choose a AppID, Select the AppID that you just created, next, submit the request file that we generated in the first step, and then the certificate is completed. Download down can be.
4. download to a CER file, we must not use the background directly. Need to do a consolidation. Here's an example of PHP. Double-click the certificate, or drag and drop the certificate into the keychain.
This way, the push certificate is found in the keychain, the first name is the same, followed by your bundle name. Next, the CER certificate is converted into a PEM file for use in the background. Right click on the certificate, select "Export" and place it locally. A file with a suffix of p12 will be obtained. Then open MacOS under the command line program, run the following commands, which will let you enter the password, is not visible, enter the line after entering, the screen is not see the input characters, will enter several times the password, so it is recommended each password to lose the same.
OpenSSL pkcs12-clcerts-nokeys-out filename a.pem-in Apple certificate. P12
OpenSSL pkcs12-nocerts-out filename b.pem-in Apple certificate. P12
Finally, the two files that were removed with the certificate are combined: Cat file name A.PEM filename B.PEM > Lltpush.pem
The final LLTPUSH.PEM is what we need, for backstage use.
In this way, it is basically done. Before writing the background code, you also need to go to the developer.apple.com to build a certificate and a description file, the point is to choose the support of the push AppID, is the one we just created. This way, we can really debug the machine. To summarize, get three things on the developer.apple.com.
At the beginning, the background is built in the SAE cloud, the push code is not necessary, just call the SAE packaged a class can be. So, to use the SAE, to register an application, and then go to the Application home page, you have to configure the certificate to use the push. This is very simple, just look at the SAE's push instructions documentation is OK.
To this settings page, upload the certificate, password to write the password you fill in the command line. Remember your certificate number, because when you write push, you choose which certificate to use to send the push.
SAE does not introduce more, look at the document below. Start writing the code now and create a new PHP page. Look at the following code.
1<?PHP2 3 $cert _id= 1686;4 $device _token=$_request[' Device_token '];5 $message=$_request[' Message '];6 7 //This is a simple example of a more complex message format Please see the official Apple documentation8 $body=Array(9' APS ' =Array(' alert ' =$message, ' badge ' = 1, ' sound ' = ' In.mp3 ')Ten ); One $apns=NewSaeapns (); A //push message to SAE push server - $result=$apns->push ($cert _id,$body,$device _token ); - the if($result&&Is_array($result) ){ - $dic=Array(' success ' = ' 0 '); - EchoJson_encode ($dic); - //Var_dump ($result); +}Else { - $dic=Array(' success ' = ' 1 '); + EchoJson_encode ($dic); A //Var_dump ($apns->errno (), $apns->errmsg ()); at } -?>
This interface, need two parameters, one is the device_token of the device, and there is the message content you want to push. Then call the SAE method to do it. The send successfully returned the JSON string. The complex code will not be said.
This is just a test demo, the real push is the background judgment. We're not going to tune the interface.
Let's get here.
Remote push under IOS8