"IOS Push omni-directional interpretation (c)" "-A minimalist demo, and test the push

Source: Internet
Author: User
Tags php script

This is a tutorial from Raywenderlich, the content is informative. The structure is simple and thorough, the explanation is gradual, the article quality is superior. It's a difficult blog post. Use half a bottle of English to translate a bit:


1. "IOS Push full resolution" (i) Overview of the push
2. "IOS Push omni-directional analysis" (ii) Generate push certificate, generate provisioning profile
3. "IOS Push omni-Directional analysis" (iii) a minimalist demo, and test the push. (Ben Boven)

Here to see the original version of a minimalist demo

So far, it's not enough to be exciting, but those preparations are necessary. This tutorial shows you how to generate a certificate in detail, because a certificate is used every day, and it is not good to push without a certificate. You just got a certificate that 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:


(construction project)

This is how I fill in:

Product Name:pushchat

Organization Name:ray Wenderlich

Company Indemnifier:com.hollance

Device Family:iphone

Product name, together with company Identifier, forms the bundle ID. In my case, "com.hollance.PushChat". You should select a product name and company Identifier, that is, the app that was generated from the previous provisioning portal ID (for example, Com.yourname.PushChat). Make sure to check "User stroyboards" and "Use Automatic Reference counting". OK, finish creating the project. Into 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] Registerforremotenotif Icationtypes:
		(Uiremotenotificationtypebadge | Uiremotenotificationtypesound | Uiremotenotificationtypealert)];
 
    Return YES
}

Function Registerforremotenotificationtypes is a high-speed iOS the app wants to receive push notifications. Compile and run the app. You should be running on the real machine because the emulator does not support push notifications. Xcode will automatically choose a new provisioning profile. When the program starts and registers a push notice, the phone bounces a message informing the user that the app wants to send a push notice.


(Send push notification reminder)

App just pops up one such dialog to ask the user. If the user clicks OK, then everything is set up. If you click Don T Allow, the app will never get a push notification. The user can change the push settings in the phone settings.


(Set push notification)

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


(Set the push notice in detail)

Your app can set exactly what kind of push, such as:

Uiremotenotificationtype enabledtypes = [[UIApplication sharedapplication] enabledremotenotificationtypes];

Add a logic to the 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 registers with a remote push notification, it returns a device token. This is a 32-byte number that can uniquely identify your device. Yes, device token imagined 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 token. It's enough for us to know it's a 32-byte long data.

This is the change to the app project, and one more thing to do, you can see the push notice. send first push notification

As I've mentioned many times before, you need to get a server to send push notifications. Here for testing, you don't have to set up a server. I've written a simple PHP script that can connect to APNs and send a push notification that specifies token. You can run it on a Mac.

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

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

Copy the device token from the app's running results to the $devicetoken variable and remove the blanks and carriage returns. is 64 hexadecimal characters altogether. Place the password of the private key in the $passphrase variable; 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 the certificate and 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 received the first push notice.


(push test succeeded)

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

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.