iOS push service, PHP server

Source: Internet
Author: User
Tags openssl x509 php server

This article reprinted to http://my.oschina.net/AStar/blog/176531

Generate certificate
Certificate Generation Reference: Https://parse.com/tutorials/ios-push-notifications
will generate three files
Xxx.cer
Xxx.certsigningrequest
Xxx.p12


Using PHP for push needs
Push certificate generation.
To generate a PEM step:
1. Download the push certificate Aps_developer_identity.cer file that you configured.
2. Convert the. cer file to the. pem file:
OpenSSL x509-in aps_developer_identity.cer-inform der-out Pushchatcert.pem
3. In the. P12 to the. Pem file that you exported your "key" Push certificate to:
OpenSSL pkcs12-nocerts-out pushchatkey.pem-in PUSHCHATKEY.P12
Enter Import Password:
MAC verified OK
Enter PEM Pass Phrase:
Verifying-enter PEM Pass Phrase:
4. Merging two PEM files: Cat Pushchatcert.pem Pushchatkey.pem > Ck.pem
5. Test whether the certificate is available:
OpenSSL s_client-connect Gateway.sandbox.push.apple.com:2195-cert Pushchatcert.pem-key PushChatKey.pem


Client Push processing, the following add **************** in appdelegate text
Whether to support registration push
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
/*............... */
Types of message push support
Uiremotenotificationtype types = (uiremotenotificationtypebadge| uiremotenotificationtypesound| Uiremotenotificationtypealert);
Register message push
[[UIApplication sharedapplication]registerforremotenotificationtypes:types];
}


Send push ideas: First get the devicetoken of the phone, and then send to our server
Get Devicetoken Success
-(void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken
{
NSLog (@ "register device token");
NSLog (@ "Devicetoken: {%@}", Devicetoken);
The operation here is to send device token to the server
NSString *TOKENSTR = [Devicetoken description];
NSString *pushtoken = [[[Tokenstr stringbyreplacingoccurrencesofstring:@ ' < ' withstring:@ ']
stringbyreplacingoccurrencesofstring:@ ">" withstring:@ ""]
stringbyreplacingoccurrencesofstring:@ "" withstring:@ "";


Register to our server
NSString *url = @ "http://api.xxxxxxcom/index.php?r=site/saveIphoneDeviceToken&device_token=";
url = [url stringbyappendingformat:@ "%@", Pushtoken];
[[Afoscclient Sharedclient]getpath:url Parameters:nil
success:^ (afhttprequestoperation *operation, id responseobject) {
NSLog (@ "registered successfully");
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
NSLog (@ "registration failed");
}];
}


Then the process receives the push message
Registration Message Push failed
-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) error
{
NSLog (@ "Register message push failed");
NSLog (@ "Register Remote notifications error:{%@}", [Error localizeddescription]);
}


Handling received message pushes
-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo
{
NSLog (@ "Receive Remote notification:%@", userInfo);
NSLog (@ "%@", [UserInfo objectforkey:@ "APS"]);
Nsdictionary *aps = [UserInfo objectforkey:@ "APS"];
NSLog (@ "%@", [APS objectforkey:@ "alert"]);
NSString *msg = [APS objectforkey:@ "alert"];
NSLog (@ "%@", [UserInfo objectforkey:@ "alert"]);
Uialertview *alert =
[[Uialertview alloc] initwithtitle:@ "Warm Tips"
Message:msg
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alert show];
[UIApplication sharedapplication].applicationiconbadgenumber = 0;//Set Badge to 0
}




Server-Side push.php****************


<?php
Mobile registration app returns unique Devicetoken
$deviceToken = ' eb87b209 6395a040 a1cf85e5 15b6e972 20ec883f 6b3ac80f 02b92a1a ';
CK.PEM Clearance Password
$pass = ' [email protected] #23 ';
Message content
$message = ' This is a push test notification 1 '. time ();
Badge, I don't know what it is.
$badge = 1;
Sound I don't know what it is (maybe a beep when I push a message to the phone)
$sound = ' duck.wav ';
Construction notification Payload (i.e. some information contained in the notification)
$body = Array ();
$body [' aps '] = Array (' alert ' = = $message);
if ($badge)
$body [' APS '] [' badge '] = $badge;
if ($sound)
$body [' APS '] [' sound '] = $sound;
Convert array data to JSON data
$payload = Json_encode ($body);
echo strlen ($payload), "\ r \ n";


The bottom of the writing is the death of the wording, generally do not need to modify,
The only thing to be modified is: ssl://gateway.sandbox.push.apple.com:2195 This is the sandbox test address, ssl://gateway.push.apple.com:2195 official release address
$ctx = Stream_context_create ();
Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', DirName (__file__). ‘\\‘ . ' Dev_ck.pem ');
Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $pass);
$fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $err, $errstr, $, stream_client_connect, $ CTX);
if (! $fp) {
Print "Failed to connect $err $errstr \ n";
Return
}
else {
Print "Connection ok\n<br/>";
}
Send Message
$msg = Chr (0). Pack ("n", 32). Pack (' h* ', Str_replace (', ', $deviceToken)). Pack ("n", strlen ($payload)). $payload;
Print "Sending message:". $payload. "\ n";
Fwrite ($fp, $msg);
Fclose ($FP);
?>


PHP server If the problem, please check whether to open the SSL module
Apache needs to turn on the SSL module, and by looking at the official Apache documentation, the use of SSL requires Apache to open three support modules, respectively:
Mod_include
mod_cgi
Mod_expires
* Note: iOS push messages have a 256 character length limit; Out of range cannot be sent

iOS push service, PHP server

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.