ios-Push Notification detailed

Source: Internet
Author: User
Tags ssl certificate

This is a compiled article from Parse.com's iOS development tutorial, and the author also provides video tutorials. This article will lead developers to explore the depths of iOS push notifications and learn how to configure iOS push notifications in one step.

Introduce a little background information

As we all know, using push notifications is a great way to add real-time message notifications to your app. The end result is that there is always a pleasing intimacy between the developer and the user.

Unfortunately, iOS push notifications are not so easy to navigate, and often make developers exhausted, frustrated, and unsustainable. Now, the Savior has come! As long as you read through this tutorial, you can get rid of these troubles, become a vibrant, jingzhuang (robust I mean! ) of the developer.

Preparatory work before the official start

Let's start this wonderful tutorial by creating an affiliate app ID and an SSL certificate to develop provisioning profiles from the Apple Developer website. Next, let's look at how the parser (parse app) is configured in the parsing site under iOS. Website. Finally, we put these theories into practice, creating an app with push functionality and pushing messages to the user.

Before starting this wonderful journey, be sure to note that the iOS simulator is not supported for push, so you have to find a way to get a real machine. There is also the Apple developer agreement that can be developed and tested on the real machine.

Create an SSL certificate

First, you need to create an app ID and an associated SSL certificate on the Apple developer site, and with this certificate, the resolution server will find your app ID and then push the notification to the app.

Create a certificate request

First of all, we need to have a certificate signing request file, after which it makes sense to create an SSL certificate. Here's how to create a file:

1. Run Keychain Access on Mac (keychain)

2. Select Keychain Access > Certificate Assistant > Request a certificate from a certification authority

3. Enter your name and email address, CA email address By default, do not attempt to make any changes.

4. Select "Save to Hard disk" so that the certificate request file you just created will be downloaded to your computer desktop.

Creating and tuning an app ID

Each iOS program installed on your developer device needs a unique app ID, for convenience, the app ID is named after the reverse path rule, like Com.parsesampleapp, but be aware that the app ID cannot contain an asterisk ("*"). The steps to create are as follows:

1. Login to the website of Apple Developer Member Center and go to iOS Provisioning portal.

2. Click App IDs from the left sidebar.

3. Select the new app ID, and then create an app ID. Make sure that there are no asterisks in the bundle identifier column.

4. Locate configure under your App ID and select.

5. Tick "Enable for Apple push Notification Service" and then click Configure under Development Push SSL certificate, and the Apple push will appear. Notification Service SSL Certificate Assistant Setup Wizard.

6. Click Continue to continue, then click Choose File to select the. certsigningrequest file you just created.

7. Click Generate to start the build, and then click Download to download the generated SSL certificate.

8. Install the downloaded SSL certificate through the Keychain program.

9. Next to the "My Certificates" option, locate the certificate you just installed with the name "Apple development IOS Push services:xxx".

10. Double-click the certificate, select Export, and the exported file suffix is. p12. Be careful at this time! Do not add anything when the password prompt appears.

One thing to note here is that we've just opened the app's push notification feature in development mode, so before the app starts its official release, be sure to go through the steps fourth through Nineth and move the "development push SSL Certificate" in the fifth step "Change to" Production Push SSL Certificate ". That's perfect.

Create a provisioning Profile (configuration introduction)

Provisioning profile verifies the devices that are running the application being developed. And whether you're creating a new app ID or modifying a ready-made one, you'll have to regenerate and install the provisioning profile again. The steps are as follows:

1. Select Provisioning changes in the iOS Provisioning portal.

2. Click New profile

3. Fill in the corresponding information and make sure that the three items (developer certificate, the app ID you just created and the device for testing) are not missing and are all selected.

4. Click the download button below the actions column to download the generated provisioning profile.

5. Double-click the downloaded file, by default it is opened by the iphone Configuration utility program.

Configure the Parse App

To use the parse feature in push notifications, you must set this feature to on, and then upload the above-created push SSL certificate. The steps are as follows:

1. Locate your parse app on the parse website and select the Settings tab.

2. Under iOS Push Notification settings, click Choose File, and then upload the. p12 file that you previously exported with keychain.

3. If you want users to be able to send push notifications, we need to tick yes on the Client Push enabled option. This feature is very useful for real-time chatting software, which we now tick, and of course developers need to decide whether or not to turn on this feature at their own discretion.

4. Click Save.

At this point, all the prerequisites are finished, immediately into the most exciting to create a push notification application link, drink saliva, open to engage.

Create an app with push notifications

First, we need to set some settings on the Xcode project to make sure that the app ID and provisioning profile are set to a good state. Do you develop,

1. Under the Supporting Files folder, select Projectname-info.plist, modify the bundle identifier options in the right view, and the app you created yourself The ID remains the same (shape: Com.parsesampleapp).

2. In the menu on the left, select the project file you just created, find the build settings below and then search for the code Signing Identity.

3. Set all values for the corresponding provisioning profile.

4. Select the project name below the left-hand side targets, and find the build Settings again to the code Signing identity area to ensure that all values are consistent with the new provisioning profile.

Code link

The next step is to get into programming mode. We need to make minor changes to the application proxy (app delegate) so that our app can accept push notifications. The steps are as follows:

1. Registering the device needs to be called in the app Delegate [Application:didfinishlaunchingwithoptions:] method [Application Registerforremotenotificationtypes:] method, the code is as follows:

  1. -(BOOL) Application: (UIApplication *) application
  2. Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions
  3. {
  4. ...
  5. Register for push notifications
  6. [Application Registerforremotenotificationtypes:
  7. Uiremotenotificationtypebadge |
  8. Uiremotenotificationtypealert |
  9. Uiremotenotificationtypesound];
  10. ...
  11. }

2. Once successful, the method above will execute the callback method in app delegate [Application:didregisterforremotenotificationswithdevicetoken:]. We need to implement this method and use it to inform the parse of our device information. The code is as follows:

  1. -(void) Application: (UIApplication *) application
  2. Didregisterforremotenotificationswithdevicetoken: (NSData *) Newdevicetoken
  3. {
  4. [Pfpush Storedevicetoken:newdevicetoken]; Send Parse the device token
  5. Subscribe This user to the broadcast channel, ""
  6. [Pfpush subscribetochannelinbackground:@ "" block:^ (BOOL succeeded, Nserror *error) {
  7. if (succeeded) {
  8. NSLog (@ "successfully subscribed to the broadcast channel.");
  9. } else {
  10. NSLog (@ "Failed to subscribe to the broadcast channel.");
  11. }
  12. }];
  13. }

3. The broadcast channel (broadcast channel) is used to connect all users at the same time, so many times developers may need to create more precise channels themselves. Once the push notification is accepted but the app is not in the foreground, it will be displayed in the iOS push center. Conversely, if the app is just in the active state, it is handed to the app to handle itself. Specifically, we can implement the [Application:didreceiveremotenotification] method in app delegate. The sample code simply sends this requirement to parse to process, and parse creates a modal alert to display the push content.

    1. -(void) Application: (UIApplication *) application
    2. Didreceiveremotenotification: (nsdictionary *) UserInfo {
    3. [Pfpush Handlepush:userinfo];
    4. }

OK, now start running on your iOS device and if everything goes well, you can see a modal alert request permission from the user to the push notification.

Send push Notifications

Send from parse website

Parse allows you to send push notifications from the parse website, both API and SDK. To find the Parse app and select the Push Notifications tab, you can add a message to the text box and broadcast it to the user. You can use the parse Web API to send a push to any channel by sending a POST request. The following example is a broadcast notification with the content "Hello World", which is sent using curl.

    1. Curl-x POST "Https://api.parse.com/1/push"-H "Content-type:application/json" \
    2. --data ' {"key": "Your_push_master_key", "channel": "", "type": "ios", \
    3. "Data": {"alert": "Hello world!"}} '

Send from App

Sending from the app requires the Client push enabled feature in the Parse app to open. There are a variety of ways to send push notifications from your app, and you can find everything in iOS API documentation.

    1. Broadcast "Hello World"
    2. [Pfpush sendpushmessagetochannelinbackground:@ "" withmessage:@ "Hello world!"];

ios-Push Notification detailed

Related Article

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.