I used Ray Wenderlich's tutorial to help me in this. I ran through all of the setup, and tests using the SimplePush.php that he included, but with my device token. When I ran it from Terminal, it worked perfect! I got the notification instantly. So, I decided to run a more real-world test.
My app already uses a PHP file online to update the count by 1 in an XML file I have built that it uses. So, I copied the PHP code from SimplePush.php and pasted it in with the rest of the code that accomplishes this. I then uploaded my ck.pem into the same directory online. However, the Push never gets delivered. Here is the php in full:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$title = $_POST['title'];
$xml = simplexml_load_file("URL.xml") or die("Not loaded!\n");
$responses = $xml->xpath("//channel/item[title = '$title' and first_name = '$first_name' and last_name = '$last_name']/prayer_warriors");
$responses[0][0] = $responses[0] + 1;
echo($responses);
$xml->asXML("Test.xml");
// Put your device token here (without spaces):
$deviceToken = 'MyDeviceToken';
// Put your private key's passphrase here:
$passphrase = 'Mypassphrase';
// Put your alert message here:
$message = 'Someone just pledged to pray for you!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>
Does it have something to do with this line?
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
It all works when it is local, but putting the php online along with my ck.pem and running the script doesn't work right.
More INFO:
In code, when I run the PHP, I simply have a POST command. Is that the issue, that since I'm posting, nothing is getting ran from the rest of it?
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"URL.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"Response%@",response);