iOS development, push message steps

Source: Internet
Author: User

1. Pre-preparation

Before learning to push the development, developers need two things, (1), iphone real computer, because the simulator does not support push (2), paid developer account.

2. Create a new project and select the single View application template.

3. Registration Notice (registration Notification)

(1) Add "register push" code to the Application:didfinishlaunchingwithoptions: method of the Appdelegate file.

-  (BOOL) Application: (uiapplication *) Application didfinishlaunchingwithoptions: (NSDictionary  *) launchoptions {    // register for remote notifications     if  (Ios8_or_later)  {        [[ uiapplication sharedapplication] registerforremotenotifications];         UIUserNotificationSettings *settings = [UIUserNotificationSettings  Settingsfortypes: uiusernotificationtypealert | uiusernotificationtypesound categories:nil ];        [[uiapplication sharedapplication]  registerusernotificationsettings:settings];    } else {         [[UIApplication sharedApplication] registerForRemoteNotificationTypes:  Uiremotenotificationtypealert | uiremotenotificationtypesound];    }} 

This way the iOS operating system knows that the application will need to handle the content of the push message.

With the code above, the iOS operating system contacts Apple's APNs server and obtains a device token that distinguishes every hardware device that runs the app. This device token is used by your own server to send push messages to the appliance, in the way that your server packs device tokens and messages that need to be pushed to the Apple server, and then Apple's APNs server is responsible for distributing the pushed content to the corresponding device.

What you need to know is that device token is different on every app, and it will change over time for the same app. So Apple recommends sending device tokens to your server backend every time the app is launched to ensure device token is up-to-date.

4, judge the registration notice failure or success

-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {NSLog (@ "Did Register for Remote notifications with Device Token (%@)", Devicetoken);} -(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error    {NSLog (@ "did Fail to Register for Remote notifications");    NSLog (@ "%@,%@", error, error.localizeddescription); }

5. Receive notification messages for message processing

If a notification event is received, then the application:didreceiveremotenotification in the Uiapplicationdelegate interface will be executed, and you can get the push information here and then make the appropriate processing.

-  (void) application: (uiapplication *) application  Didregisterforremotenotificationswithdevicetoken: (nsdata *) devicetoken{    nsstring  *deviceTokenStr = [[[[deviceToken description]                                    stringbyreplacingoccurrencesofstring: @ "<"   withstring: @ ""]                                   stringbyreplacingoccurrencesofstring: @ ">"  withstring: @ ""]                                  stringbyreplacingoccurrencesofstring: @ " "  withstring: @ ""];    nslog (@ "Remove device  token spaces and <> characters:%@ ", devicetokenstr);//It is only in the projects that I am involved in.

6, Production certificate, using the real machine for push message development

At this time , if the project is run, then Application:didfailtoregisterforremotenotificationswitherror will execute, because the emulator does not support push, need to produce a certificate, Use the real machine for push development.

7, SSL certificate certificate production

In the Apple Developer Center, create an app Id

The push certificate must use the exact (EXPLICIT) app IDinstead of the blur (Wildcard) app ID.

As shown,

Tick the push notifications service, as shown in

Then follow the process of creating the certificate, using the app ID that you created, because it takes a lot, not to repeat it here.

Original link: http://code.tutsplus.com/tutorials/setting-up-push-notifications-on-ios--cms-21925

iOS development, push message steps

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.