iOS development: Using PHP to implement iOS push

Source: Internet
Author: User
Tags array implement key openssl pack php code pkcs12 return

iOS push messages are a very useful feature, and many applications have this capability to become the core of the data flow for real-time applications. So how do we use PHP to do the push service for iOS? The following article will give you a detailed explanation.

The IOS message push mechanism can be used to realize and discuss the IOS message push mechanism.

First, you need a PEM certificate that needs to be consistent with the development-time signature. The specific generation of the PEM certificate method is as follows:

1. Login to the IPhone Developer Connection Portal (http://developer.apple.com/iphone/manage/overview/index.action) and click App IDs
2. Create an Apple ID. Wildcard IDs cannot be used with push Notification Services. For example, Com.itotem.iphone
3. Click "Configure" next to the Apple ID, generate a signature upload according to the steps in the wizard, and then download the generated license.
4. Double-click the. cer file to import your aps_developer_identity.cer into the keychain.
5. Start the Keychain assistant on the Mac, and then select the Certificates category in the login keychain. See an extensible option "Apple Development Push Services"
6. Extend this option and then right-click "Apple Development push Services" > Export "apple Development push Services ID123". Save as a apns-dev-cert.p12 file.
7. Extended "Apple Development Push Services" to the "Private Key" to do the same operation, save as a apns-dev-key.p12 file.
8. Convert these files to PEM format via a terminal command: OpenSSL pkcs12-clcerts-nokeys-out apns-dev-cert.pem-in APNS-DEV-CERT.P12
OpenSSL pkcs12-nocerts-out apns-dev-key.pem-in APNS-DEV-KEY.P12
9. Finally, you need to synthesize the keys and license files as APNS-DEV.PEM files, which need to be used when connecting to APNs:
Cat Apns-dev-cert.pem Apns-dev-key-noenc.pem > Apns-dev.pem

 

The PHP code is as follows:

  1. <?php
  2. $deviceToken = $_post[' token ']; Get token
  3. $body = Array ("APS" => Array ("Alert" => ' message ', "badge" => 2, "sound" => ' Default ')); Push way, including content and sound
  4. $ctx = Stream_context_create ();
  5. If you have problems finding a PEM path on a Windows server, the path is modified in such a way that:
  6. $pem = DirName (__file__). ‘/’ . ' Apns-dev.pem ';
  7. Linux server directly write the path of the PEM
  8. Stream_context_set_option ($ctx, "SSL", "Local_cert", "Apns-dev.pem");
  9. $pass = "123123";
  10. Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $pass);
  11. There are two servers to choose from, if it is development testing, select a second sandbox server and use Dev's PEM certificate, if it is a release, use the product's PEM and choose a formal server
  12. $fp = Stream_socket_client ("ssl://gateway.push.apple.com:2195", $err, $errstr, Stream_client_connect, $ctx);
  13. $fp = Stream_socket_client ("Ssl://gateway.sandbox.push.apple.com:2195″, $err, $errstr, Stream_client_connect, $ CTX);
  14. if (! $fp) {
  15. Print "Failed to connect $err $errstrn";
  16. Return
  17. }
  18. Print "Connection ok\n";
  19. $payload = Json_encode ($body);
  20. $msg = Chr (0). Pack ("n", 32). Pack ("h*", Str_replace (",", $deviceToken)). Pack ("n", strlen ($payload)). $payload;
  21. Print "Sending message:". $payload. "\ n";
  22. Fwrite ($fp, $msg);
  23. Fclose ($FP);
  24. ?>

And then

Here the certificate is ready, and next, we'll create a new test project in Xcode, and note that the bundle identifier for the Setup project must be the same as the app ID built above.


Add code to Didfinishlaunchingwithoptions

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

{

[Self.window makekeyandvisible];

[[UIApplication Sharedapplication] Registerforremotenotificationtypes:uiremotenotificationtypebadge Uiremotenotificationtypesound Uiremotenotificationtypealert];

return YES;

}

-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) PToken {

NSLog (@ "Regisger success:%@", PToken);

}

-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo{

Uialertview *alert=[[uialertview alloc]initwithtitle:@ "Push notification message:@" message "delegate:selfcancelbuttontitle:@" Cancel "Otherbuttontitles:nil, nil];

[Alert show];

[Alert release];

}

-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error {

NSLog (@ "Regist fail%@", error);

}

Next we visit http://localhost/push/push.php

The iOS device will receive a push message.

In addition, the method of removing the mark is to add in the Viewdidapper

int badge = [UIApplication sharedapplication].applicationiconbadgenumber;

if (Badge > 0)

{

badge--;

[UIApplication sharedapplication].applicationiconbadgenumber = badge;

}



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.