IOS push details

Source: Internet
Author: User

IOS push details

1. About push notifications

 

Push notifications, also known as remote notifications, are introduced after iOS 3.0. When the program is not started or is not running in the foreground, a new message is sent from an external server to the application. Generally, when you want to display a message or download data, the Notification is sent by a remote server (Program provider) and then sent through Apple's Push Notification Service (apns) the program pushed to the device.

A new message may be a piece of information, a calendar that is about to expire, or a copy of new data on a remote server. When displayed on the system, you can display a warning message or a number on the program icon, and play a warning sound. Once the user notices that the program has new information, time, or data, they can run the program and access the new content. You can also ignore the notification, and the program will not be activated.

On the iPhone, iPad, and iPod touch, only one app is running on the front desk at the same time. When most programs run in the background, they can respond to content that users are interested in (such as timing, or data ). Push notifications allow the program to notify users when these events occur.

As a provider for Program development and deployment push notifications, you must obtain the SSL certificate through the iOS Developer Program Portal. Each certificate is limited to one program and identified by the bundle ID of the program. Certificates can be used in two ways: for sandbox (for development and testing) and for publishing products. The two running environments have their own specified IP addresses and require different certificates. You must also obtain the respective provisioning profiles for two different environments.

APNS provides two basic services: Message push and feedback.

Message push: Uses streaming TCP sockets to send push notifications to APNs as binary data. Message pushing has two interfaces for development and testing, sandbox and product release, each with its own address and port. No matter which interface you use, you must use TLS or SSL to establish a secure channel. The provider prepares the notification information and sends it to APNs through this channel. Note: sandbox: gateway.sandbox.push.apple.com: 219 product interface: gateway.push.apple.com: 2195

Feedback service: You can get a sending failure record for a program. The provider should use the feedback service to periodically check which devices have never received the notification, and do not need to send the notification to these devices repeatedly, reducing the burden on the push server. Note: sandbox: feedback.push.apple.com: 2196 product interface: feedback.sandbox.push.apple.com: 2196

Ii. Working Mechanism of Apple Push Notification

Your client sends messages to your app's apns, and apns sends messages to your mobile phone. This is identified based on the token. The mobile phone is sending messages to your application.

  • First, the application registers for message push.
  • For IOS and APNS Server, deviceToken is required. The application accepts deviceToken.
  • The application sends the deviceToken to the PUSH Provider ).
  • The server program sends a message to the APNS service.
  • The APNS Service sends messages to iPhone applications.

    1. Provider refers to the Push server of an iPhone software, which we will develop.

    2. APNS is the abbreviation of Apple Push Notification Service (Apple Push server), which is an Apple server.

     

    There are three phases:

    Stage 1: The push server (provider) packs the message to be sent and the ID of the target iPhone and sends it to APNS;

    Stage 2: APNS searches for the iPhone with the corresponding identifier in its registered Push Service iPhone list and sends the message to the iPhone;

    Stage 3: The iPhone transmits the sent message to the corresponding application, and a Push notification is displayed according to the settings.

    In any iOS application development, developers always assume a role in the team: Team Manager, Team administrator, and team members. The iPhone Development Certificate and configuration file are completed by different roles. Usually, the team manager is the only person in the team who can create the Development SSL Certificate (sandbox environment) and Distribution SSL Certificate (product environment. Both the team manager and the team administrator can create the Development and Distribution configuration files. Team members can only download and install certificates and configuration files (! The concept of the certificate and configuration file should be distinguished here: Certificate, namely, Certificates, which is mainly a signature Key generated using the Access Key Chain. The configuration file, Provisioning profiles, is a specific code signature file generated based on different apps. The difference between the two is that a developer account can only have one Certificates (one for Development and one for Distribtuion), but there can be multiple Provisioning profiles, and these Provisioning profiles are usually created with the same Certrificates ). The process is described in the following sections.
    Note: the iOS Provisioning Portal is provided to all iOS developers. Create an SSL Certificate and key
    In the iOS Provisioning Portal of iOS Dev Center, the team manager selects the AppID of the corresponding APNs. Follow these steps to create an SSL certificate:
    1.
    Click App IDs on the left side of the window.
    A page that displays the IDs currently valid applications is displayed. The ID of each application contains its Bundle ID, and the ID contains ten strings generated by Apple before the Bundle ID. The team administrator must enter the Bundle ID. For each certificate, it must match a specific Bundle ID. You cannot use the "wildcart" Application ID. That is, the Bundle ID of the application to use the push notification cannot be created using.
    2.
    Locate the Application ID (associated with the Development Provisioning Profile) for which you want to create an SSL Certificate, and click the Configure button.
    You must make sure that the "available" status is displayed under the apple push notification service bar under the App ID to configure the APNs certificate for the App. 3. On the App ID configuration page, check that the "Enable for Apple Push Notification service" check box has been selected, and click the "Configure" button below.
    Click this button to start the APNs assistant. It guides you through a series of steps to complete the operation. 4.
    The first step is to start the Keychain Access application and generate a Certificate Signing Request (CSR) file.
    The following is an introduction from the certificate assistant. After generating the CSR, click Continue on the Keychain Access to return to APNs Assistant.
    When you create a CSR, Keychain Access generates a private and public encryption key pair. By default, private keys are stored in your Login keychain. The public key, including the CSR, will be sent to the configuration server. When the configuration server sends the certificate back to you, one of the entries in the certificate will be a public key.
    5.
    On the Submit Certificate Signing Request panel, click Choose File. Navigate to the CSR file you created and select it.
    6.
    Click Generate.
    When the Generate Your Certificate panel is displayed, Assistant configures and generates Your client SSL Certificate. If the generation is successful, the message "Your APNs Certificate has been generated." is displayed .". Click Continue to proceed to the next step.
    7.
    In the next panel, click Download Now to Download the Certificate file to the local folder. Find the download location of the file and double-click the Certificate file (the Certificate file contains a. cer extension) to install it in your keychain. Click the Done button on APNs Assistant.
    Double-click to load and start Keychain Access. Make sure that you have installed the certificate in the login keychain of the computer you used for development. In Keychain Access, make sure that the ID used by your certificate matches the Bundle ID of your application. The SSL Certificate of APNs should be installed on your notification service.
    After you complete these steps on the Configure App ID page of the iOS Dev Center Portal, your certificate will change to green and "Enabled" will be displayed ".
    To create a certificate for the product environment, repeat the preceding steps, but remember the Application ID of the selected product Certificate. The team manager or team manager must create a configuration certificate (Development or Distribution) for remote notification Development on the server ). The configuration file is a collection of devices verified by application developers and development teams and used to test applications. The configuration file contains the certificate, device identifier, application Bundle ID, and all rights, including. All team programs need to install the configuration file on the device that runs and tests their application code.
    Note: For details about how to create a configuration file, see the programming user guide.
    To download and install the configuration file, the team program must complete the following steps:
    1.
    Go to the Provisioning portal page of iOS Dev Center.
    2.
    Create a new configuration file that contains the App ID you registered for APNs.
    3.
    Modify any existing configuration files before you download the new configuration file.
    You must modify some minor parts of the configuration file (such as switching options) to generate a new configuration file. If the configuration file is not "damaged (dirtied)", you should not give the original configuration file any push rights.
    4.
    Find the download directory of the file and drag the configuration file (usually a. mobileprovision extension file) to the icon of the Xcode or iTunes application.
    Optional. You can copy the configuration file ~ /Library/MobileDevice/Provisioning Profiles directory. If the current directory does not exist, a new directory is generated.
    5.
    Verify that the configuration file's rights are correct. To this end, open the. mobileprovision file in the text editor. The content of this file is an XML text. View the dictionary value at the position of the aps-environment key. For a configuration file in development mode, the value should be development. For the configuration file in release mode, the value of this character should be production.
    6.
    In the Organizer window of Xcode, view the Provisioning Profiles section and confirm that the certificate has been installed on your device.
    When you compile the project, the binary file now uses the Private Key signature of the certificate.
    4.2.3
    Install the SSL Certificate and key on your server
    You must install the SSL publishing certificate and the private encryption key you obtained before to the server that needs to run the provider code, and connect the server to the APNs sandbox or product environment. To do this, follow these steps: 1.
    Open the Keychain Access entity and click My Certificates category on the left panel.
    2.
    Find the certificate you want to install and open the relevant content.
    You will see the certificate and private key.
    3.
    Select the certificate and key, select File> Export Items, and Export them to the. p12 File ).
    4.
    If the server uses the Buby or Perl language, it is easier to process files in the format of personal information interchange. To convert a certificate to this format, follow these steps:
    A)
    In Keychain Access, select the corresponding certificate and select File> Export Items. Select ". p12 ). Select a Save address and click Save.
    B)
    Load the terminal application and enter the following command:
    Openssl pkcs12-in CertificateName. p12-out CertificateName. pem-nodes
    5.
    Copy the. pem Certificate file to the new computer and install it to the appropriate place. The preparation has been completed. The following is the client step. An application must register the apple push notification service on the device (iOS device or Mac computer) to receive remote notifications from the program provider.
    The registration process involves three steps: An application must register the apple push notification service on the device (iOS device or Mac computer) to receive remote notifications from the program provider.
    The registration process includes the following three steps:
    1.
    The program calls registerForRemoteNotificationTypes: method.
    2.
    Delegate implementation application: didRegisterForRemoteNotificationsWithDeviceToken: Method to receive device tokens.
    3.
    Pass the device token as a non-object (binary value) to the program provider.
    1.
    The program calls registerForRemoteNotificationTypes: method.
    2.
    Delegate implementation application: didRegisterForRemoteNotificationsWithDeviceToken: Method to receive device tokens.
    3.
    Pass the device token as a non-object (binary value) to the program provider. -(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
    • {
    • Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
    • // Override point for customization after application launch.
    • Self. viewController = [[ViewController alloc] init] autorelease];
    • Self. window. rootViewController = self. viewController;
    • [Self. window setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @ "background.png"];
    • [Self. window makeKeyAndVisible];
    • /** Register the push notification function ,*/
    • [[UIApplication sharedApplication] registerForRemoteNotificationTypes :( UIRemoteNotificationTypeAlert | uiremotenotiftypetypebadge | uiremotenotiftypetypesound)];
    •  
    • // Determine whether the program is completed by the PUSH Service
    • If (launchOptions ){
    • NSDictionary * pushNotificationKey = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    • If (pushNotificationKey ){
    • UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "push notification"
    • Message: @ "this is a program started through the push window. You can process the push content here"
    • Delegate: nil
    • CancelButtonTitle: @ "OK"
    • OtherButtonTitles: nil, nil];
    • [Alert show];
    • [Alert release];
    • }
    • }
    • Return YES;
    • }

      -(Void) application :( UIApplication *) app didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {

      • NSString * token = [NSString stringWithFormat: @ "% @", deviceToken];
      • NSLog (@ "apns-> generated devToken: % @", token );
      • // Send deviceToken to our push Server
      • DeviceSender * sender = [[[DeviceSender alloc] initWithDelegate: self] autorelease];
      • [Sender sendDeviceToPushServer: token];
      • }

         

        -(Void) application :( UIApplication *) app didFailToRegisterForRemoteNotificationsWithError :( NSError *) err {

         

        • NSLog (@ "apns-> An error occurred while registering the push function. error message: \ n % @", err );
        • }

          -(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo

          • {
          • NSLog (@ "\ napns-> didReceiveRemoteNotification, Receive Data: \ n % @", userInfo );
          • // Set the number marked on the icon to 0,
          • Application. applicationIconBadgeNumber = 0;
          • If ([[userInfo objectForKey: @ "aps"] objectForKey: @ "alert"]! = NULL ){
          • UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "** push message **"
          • Message: [[userInfo objectForKey: @ "aps"] objectForKey: @ "alert"]
          • Delegate: self
          • CancelButtonTitle: @ "close"
          • OtherButtonTitles: @ "processing push content", nil];
          • Alert. tag = alert_tag_push;
          • [Alert show];
          • }
          • }

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.