"IOS Push full resolution (iii)" translation "--a minimalist demo and test push

Source: Internet
Author: User

This is a tutorial from Raywenderlich, the content is informative! The structure is simple and thorough, the explanation is gradual, article quality is superior! It's a difficult blog post! Use half a bottle of English to translate a bit:
1. Overview of Push
2. Generate a push certificate to generate the provisioning profile
3. A minimalist demo, and test push. (Ben Boven)

View the original text here

A minimalist demo

So far, it's not enough to get excited about it, but it's necessary to get ready for the job. This tutorial shows you how to generate a certificate in detail, because a certificate is used every day, and no certificate push is bad. You just got a certificate, you can connect to APNs. Now let's test it out.

Open Xcode Select File,new Project. Select single View application in the panel and Next:


(Building works)

That's what I'm filling in:

Product Name:pushchat

Organization Name:ray Wenderlich

Company Indemnifier:com.hollance

Device Family:iphone

Product name and company Identifier together form the bundle ID. In my case, it's "com.hollance.PushChat". You should choose a product name and company Identifier, that is, the app that was built from the previous provisioning portal ID (for example, Com.yourname.PushChat). Be sure to tick "User stroyboards" and "Use Automatic Reference counting". OK, finish creating the project. Enter the APPDELEGATE.M. Change the code:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {//Let The device know we want to receive push notifications[[uiapplication Sharedapplication] Registerforremotenotificationtypes: (Uiremotenotificationtypebadge | Uiremotenotificationtypesound | Uiremotenotificationtypealert)];     return YES;}

function registerforremotenotificationtypes is high speed iOS the app wants to receive push notifications. Compile and run the app. You should run on the real machine because the emulator does not support push notifications. Xcode automatically selects the new provisioning profile. When the program launches and registers a push notification, the phone bounces a message informing the user that the app wants to send a push notification.


(send a push notification reminder)

The app simply pops up a dialog box asking the user. If the user clicks OK, then everything is set up. If you click "Don't allow", then the app will never receive push notifications. The user can change the settings of the push in the phone settings.


(Set push notifications)

The app's name is added to the settings, where users can enable and disable app push notifications, including badge, sounds, and alert styles.


(Set up push notifications in detail)

Your app can set which push is specific, such as:

Uiremotenotificationtype enabledtypes = [[UIApplication sharedapplication] enabledremotenotificationtypes];

Also add a logic in APPDELEGATE.M to receive the push notification

-(void) application: (uiapplication*) Application Didregisterforremotenotificationswithdevicetoken: (NSData*) Devicetoken{nslog (@ "My token is:%@", Devicetoken);}-(void) application: (uiapplication*) application Didfailtoregisterforremotenotificationswitherror: (nserror*) Error{nslog (@ "Failed to get token, error:%@", error);}

When your app is registered with a remote push notification, it returns a device token. This is a 32-byte number that uniquely identifies your device. You can think of device token as the address of a push notification. To run the app again, you should see the following output in the console:

My token is:
<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>

Token is a string of binary data that is stored in a nsdata. Apple doesn't want developers to make any changes to tokens. It is enough for us to know that it is a 32-byte long data.

App engineering changes that, and one more thing to do, you can see the push notifications.

Send the first push notification

As I mentioned many times before, you need to get a server to send push notifications. To test this, you don't have to set up a server. I have written a simple PHP script that can connect to the APNs and send a push notification of the specified token. You can run it on your Mac.

Download Simplepush code and unzip, and you need to change the simplepush.php file:

Put your device token here (without spaces): $deviceToken = ' 0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78 '; Put your private key ' s passphrase here: $passphrase = ' pushchat '; Put your alert message here: $message = ' My first push notification! ';

Copy the device token from the app project run results to the $devicetoken variable, removing the space and carriage return. Altogether is 64 hexadecimal characters. Put the password of the private key into the $passphrase variable, and set the text content you want to the $message variable.

Copy the Ck.pem to the Simplepush folder, remember! The Ck.pem file must contain a certificate and a private key. Open the command line:

$ PHP simplepush.php

If everything works, the script output is:

Connected to APNS
Message successfully delivered

After a few seconds, your device receives the first push notification.


(push test succeeded)

Note: There is nothing to see when the app starts. The push message was distributed, but it was not processed in the app.

IOS Push full resolution (III) translation "-A minimalist demo and test push

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.