Remote push under ios8.
I made a remote push yesterday. I wrote it down today and shared it with people who need it. I have referenced many articles, maybe the iOS 8 changes, but none of them can be viewed completely, so I plan to write one by myself.
I wrote it in the background and used the SAE and PHP code. It is very simple. You can send push messages to APNS by calling a class encapsulated by SAE.
First, let's talk about Apple's push mechanism. As the name implies, push means that the server sends messages to the client. In iOS, the app is suspended in the background and cannot connect to the network all the time. How can the server send messages to the client? This uses push. Apple's push mechanism only allows APNS to send push notifications. Therefore, if your background server wants to push notifications to the client, you must send the content to APNS.
If you do not know about Apple's push mechanism, you can view the information on your own. In short, the overall process is: when the APP starts, it obtains the device_token of the device and then tells your server the device_token. After the server obtains the device_token, it uses the Certificate file, initiate an SSL connection to Apple's APNS server and send a JSON string after the connection is successful. The JSON string contains device_token, notification content, and other things. After Apple's APNS receives the JSON value, it uses device_token to find the device push message.
Therefore, the workload on the iOS front-end is very simple. You only need to get the device_token and do something after receiving the notification. Then, how to get the device_token of the device, iOS8 uses a new method. It is called when the APP is started, for exampleApplication: didfinishlaunchingwitexceptions:In this method, the remote push Application Method for APP registration is called.
1 //ios82 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 is called back. The Code is as follows:
1-(void) application :( UIApplication *) application didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken 2 {3 NSString * decToken = [NSString stringWithFormat: @ "% @", deviceToken]; 4 // remove the angle brackets and spaces 5 NSMutableString * st = [NSMutableString stringWithString: decToken]; 6 [st deleteCharactersInRange: NSMakeRange (0, 1)]; 7 [st deleteCharactersInRange: NSMakeRange (st. length-1, 1)]; 8 NSString * string1 = [st stringByReplacingOccurrencesOfString: @ "" withString: @ ""]; 9 // save to local 10 NSUserDefaults * u = [NSUserDefaults standardUserDefaults]; 11 [u setObject: string1 forKey: @ "deviceToken"]; 12}
Correspondingly, there must also be a callback for registration failure. You can write the error processing code in it. here we will not give an example.
Have you ever thought that normal push notifications will be in our notification panel? If I am using this APP, the push will certainly not be in the notification panel, how can we find it. In the following callback method, when we are using the APP and receive a notification, the APP will directly callback this method and will not pop up the notification.
1-(void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo2 {3 NSLog (@ "Receive remote notification: % @", userInfo ); 4 UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: userInfo [@ "aps"] [@ "alert"] delegate: self cancelButtonTitle: @ "good" otherButtonTitles: nil, nil]; 5 [alert show]; 6 7}
In this method, you can process the messages we receive.
There are so many front-end tasks. Don't forget to hand over the device_token to your server.
We didn't need to write the server code, but we didn't need to write it. Let them go to Apple's official documentation. If you still don't understand it, find a ready-made code to study it.
Programmers are omnipotent in attacking the city lions. They will not only repair computers, but also write back-ends. Then, let's see how the background is implemented.
First, after the server obtains the device_token and sends a push request to APNS using the certificate, the problem arises. Which excavator technology is strong?
Where can I push my certificate?
A developer account is required. That's right. Like our real machine debugging certificate, we need at least 99 knives. With an account, we can apply for a certificate. Next, let's take a step-by-step look at the problem.
1. You need to generate a CertificateSigningRequest. certSigningRequest file on MacOS. Open "key string access" in the application, select "Certificate assistant"-"" request certificate from Certificate Authority "from the menu"
1 <? Php 2 3 $ cert_id = 1686; 4 $ device_token = $ _ REQUEST ['device _ token']; 5 $ message = $ _ REQUEST ['message']; 6 7 // This is a simple example, for more complex message formats, see Apple official documentation 8 $ body = array (9 'aps '=> array ('alert' => $ message, 'badge' => 1, 'sound' => 'input') 10); 11 $ apns = new SaeAPNS (); 12 // push the message to the SAE push server 13 $ result = $ apns-> push ($ cert_id, $ body, $ device_token ); 14 15 if ($ result & is_array ($ result) {16 $ dic = array ('suc Cess '=> '0'); 17 echo json_encode ($ dic); 18 // var_dump ($ result ); 19} else {20 $ dic = array ('success' => '1'); 21 echo json_encode ($ dic ); 22 // var_dump ($ apns-> errno (), $ apns-> errmsg (); 23} 24?>
This interface requires two parameters: device_token of the device and the message content to be pushed. Then you can call the SAE method. A json string is returned if the message is sent successfully. Complex code is not mentioned.
This is just a test demo. The real push is determined by the background. Instead of calling the interface.
That's it.