IOS Development-remote notification, remote push (remotenotification)

Source: Internet
Author: User

IOS remote notification, also known as remote Push, the use of frequency is very frequent, it is mainly through the Apple APNs Server initiative to find the device is pushed, the message to the user, if the corresponding program is not running, then the remote notification will go to the notification Center, displayed on the notification bar, Here are a few steps to keep track of the remote notifications I use.

First, create a push certificate.

The creation of a push certificate is very simple, similar to creating a development certificate.

1. Create the app ID, tick the explicit app ID, which is the explicit ID, so you can check the use of the following push Notification.

  

Then go straight to complete.

2. Create a push certificate, there are two kinds of push certificate, one is the development environment (Sandbox), one is the production environment (Production), the two certificates corresponding to the application of different operating state, here we test the Sandbox, here is the creation step.

Select the sandbox in the development and proceed to the next step.

Here he will take the initiative to match a list of app IDs with the push service checked, check the app ID you just created

Here, let's create a CSR file locally to generate our certificate

Creating a CSR certificate requires entering the keychain and then selecting

Click Continue, then select the location you created to get the CSR file

After the CSR file is created, go back to the certificate creation side, next to this location, Choose file selects the CSR files that you just created, then the next step is to create the certificate, then download click to open.

The push certificate for such a development environment is considered to be a complete creation.

Second, the code processing.

After the certificate is created, we need to go to the project APPDELEGATE.M to join the registration, to process the push code, as follows:

1. didfinishlaunchingwithoptions inside register remote push.

1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {2     3     //Registration Notice4 [self registerremotenotification];5     6     returnYES;7 }8 9 /**Ten * Sign up for remote notifications One  */ A- (void) Registerremotenotification - { -UIApplication *application =[UIApplication sharedapplication]; the     //iOS8 Registration APNs -     if([Application respondstoselector: @selector (registerforremotenotifications)]) { - [Application registerforremotenotifications]; -Uiusernotificationtype notificationtypes = Uiusernotificationtypebadge | Uiusernotificationtypesound |Uiusernotificationtypealert; +Uiusernotificationsettings *settings =[uiusernotificationsettings settingsfortypes:notificationtypes categories:nil]; - [Application registerusernotificationsettings:settings]; +     } A     //iOS8 before registering APNs at     Else -     { -Uiremotenotificationtype notificationtypes = Uiremotenotificationtypebadge | -Uiremotenotificationtypesound | - Uiremotenotificationtypealert; - [[UIApplication sharedapplication] registerforremotenotificationtypes:notificationtypes]; in     } -}

2. Implement some other relevant methods.

1- (void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings *) Notificationsettings2 {3 [Application registerforremotenotifications];4 }5 6- (void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {7     8NSLog (@"Devicetoken-%@", Devicetoken);9 }Ten  One //look at the official document comments, iOS7 after the implementation of this method, you can not be in the didfinishlaunchingwithoptions and didreceiveremotenotification to accept the push information separately, This one method can be done, the test is effective.  A- (void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo Fetchcompletionhandler: (void(^) (Uibackgroundfetchresult)) Completionhandler { -      - [self dealremotenotificationwithuserinfo:userinfo]; the Completionhandler (uibackgroundfetchresultnewdata); - } -  - /** + * Handle Remote notifications -  * + * @param userInfo notification content A  */ at- (void) Dealremotenotificationwithuserinfo: (Nsdictionary *) UserInfo { -      -Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@"Receive push"Message:nil Preferredstyle:uialertcontrollerstylealert]; -[Alert addaction:[uialertaction Actionwithtitle:@"Determine"Style:uialertactionstyledefault Handler:nil]]; -[[[[[UIApplication Sharedapplication]Delegate] window] Rootviewcontroller] presentviewcontroller:alert animated:yes Completion:nil]; -NSLog (@"receive remotenotification-%@", userInfo); in}

The above is the part of the code that needs to be processed.

Third, push test.

Push test, Pushmebaby is recommended here, easy to use, and the whole only need Xcode, tutorial here: http://www.cnblogs.com/leotangcn/p/4971814.html

Demo Address: Https://github.com/LeoTangCN/RemoteNotificationDemo

The above is the share, thanks for watching!

IOS Development-remote notification, remote push (remotenotification)

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.