Remote push implementation in IOS development (latest, support iOS9)

Source: Internet
Author: User

My personal project "Dingdingin" added the remote push function, according to the operation instructions to do or relatively easy to achieve, but not enough to learn the system, so this article would like to summarize the latest iOS push function, because after iOS8 push and to the previous version is different, Also want to be able to help the needs of friends. This article will start from scratch and introduce you to the principle and use of remote push functionality.

What is remote push notification

As the name implies, the notification that is pushed from the remote server to the client (requires networking) remote Push service, also known as APNs (Apple Push Notification services).

Why remote push is required in the program

1. The limitations of traditional data acquisition as long as the user closes the app, they can't communicate with the app's server and get the latest data content from the server

2. Remote push notification can solve the above problems whether users open or close the app, as long as the Internet, you can receive the Server Push remote notification

Note Points for remote push

1. All Apple devices, in the networked state, will be connected with the Apple server to establish a long connection what is a long connection as long as the network, the connection has been established

2. The effect of long connections. Example: Time calibration, system upgrade, Find My iphone

3. Benefits of long connections data is kept up to date

Push mechanism for iOS

The sections in the diagram are described below:

1.Provider: The server on which the mobile app sends the push message.

2.apns:apns (Apple push Notification Service) is an Apple message Push server

3.Client App: The app we've developed.

Your local server when you need to push a message to the app, first send the message to the Apple push server, and then by the Apple push server to send the message to the phone that installed the app.

The following diagram shows a clearer explanation of the push mechanism:

Based on the logic I'll explain to you:

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 and pushes it to the app

The specific implementation of remote push:

Our company's servers, through Apple's APNs server, find our devices and apply push messages to clients on our devices. The APNs server is how to find the application on our devices and devices, the answer is Devicetoken,devicetoken need to configure a push certificate to get, push the certificate need to know our app ID (app Bundleid, Application unique identification) and UDID (device unique identification), as well as the udid of its own server, we first hit a push certificate, login to the Developer center, click the certificate module:

Click the certificate for iOS apps (certificates)

into such an interface

Click App IDs, then click the plus sign in the upper right corner

The arrow refers to the app ID description, we assume that we wrote a very good app, Excitedapp, and then we fill in the app ID, note that the app ID here is fixed, cannot have an asterisk

Next we add the services needed by the app, then click Continue

Always click Next, Finish, find our Excitedapp in the app IDs list, we see

Find the push service is a yellow warning, then click on Edit to find the push module, we need to configure the push certificate, click the red arrow pointing to the location

We need a CSR file, the CSR is a signature request file, meaning that our Mac has the right to debug push, click Continue

Click the Continue we see

Then we open the keychain on the Mac and request the certificate from the certification authority

Top two random write, last select Save to disk

When we're done, we're ready to add the CSR file.

Finish adding, select Build

The certificate is ready, download it

It's ready!

Let's take it with us. The certificate is also assigned, the steps are the same

Open the certificate we just downloaded and add it to the keychain

Finally, we can start the whole project, and we're going to cry.

Don't forget to change the bundle ID here's a real Machine debug certificate.

Real machine debugging things here not to be tired of, so many things, we can finally get Devicetoken

Register for Push in APPDELEGATE.M

#import " AppDelegate.h "    @implementation appdelegate   @synthesize mainView = _mainview;     -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions  {      if  ([Application respondstoselector: @selector ( Isregisteredforremotenotifications)])      
Since iOS8 push is not the same as iOS7 and the following, it is necessary to add judgment to register the message push.

//IOS8//Create a uiusernotificationsettings and set the display class type of the messageUiusernotificationsettings *notisettings = [uiusernotificationsettings settingsfortypes: ( Uiusernotificationtypebadge | Uiusernotificationtypealert |uiremotenotificationtypesound) Categories:nil];                [Application registerusernotificationsettings:notisettings]; } Else{//iOS7[Application registerforremotenotificationtypes: (Uiremotenotificationtypebadge | Uiremotenotificationtypesound |Uiremotenotificationtypealert)]; } Self.window=[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; //Override point for customization after application launch. Self.window.backgroundColor =[Uicolor Whitecolor];            [Self.window makekeyandvisible]; Self.mainview= [[Mainviewcontroller alloc] Initwithnibname:@"Mainviewcontroller"Bundle:nil]; Self.window.rootViewController=Self.mainview; returnYES; }    - (void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) ptoken{NSLog (@"---token--%@", PToken); }//will receive the Devicetoken returned from the Apple server and you will need to add it to your local push server. (It's important to decide if your device can receive a push message). - (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) userinfo{NSLog (@"UserInfo = =%@", UserInfo); NSString*message = [[UserInfo objectforkey:@"APS"]objectforkey:@"Alert"]; Uialertview*alert = [[Uialertview alloc] Initwithtitle:@"Tips"Message:messageDelegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine", nil Nil];  [Alert show]; }//This function is triggered when the device receives a message from an Apple push server to display a push message. - (void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error{NSLog (@"regist fail%@", error); }    - (void) Applicationwillresignactive: (UIApplication *) Application {//Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US      Er quits the application and it begins the transition to the background state. //Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.  Games should use this method to pause the game. }    - (void) Applicationdidenterbackground: (UIApplication *) Application {//Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in       Formation to the restore your application to the it is terminated later. //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th  E user quits. }    - (void) Applicationwillenterforeground: (UIApplication *) Application {//Called as part of the transition from the background to the inactive state; Here you can undo many of the changes mad  E on entering the background. }    - (void) Applicationdidbecomeactive: (UIApplication *) Application {//Restart Any tasks this were paused (or not yet started) while the application is inactive.  If the application is previously in the background, optionally refresh the user interface. }    - (void) Applicationwillterminate: (UIApplication *) Application {//Called when the application are about to terminate. Save data if appropriate.  See also Applicationdidenterbackground:. }    @end  

Add our push certificates to the project directory

Next we use Pushmebaby to test our push. pushmebaby:https://github.com/stefanhafeneger/pushmebaby/

If you're doing the right thing, congratulations, you can get a push on your phone.

If not received, I regret to inform you, please check whether each link is properly configured.

Remote push implementation in IOS development (latest, support iOS9)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.