我使用 Ray Wenderlich 的教程来帮助我。我运行了所有设置,并使用他包含的 SimplePush.php 进行了测试,但使用了我的设备令牌。当我从终端运行它时,它运行良好!我立即收到通知。所以,我决定进行一个更真实的测试。
我的应用程序已经在线使用 PHP 文件将我构建的它使用的 XML 文件中的计数更新为 1。因此,我从 SimplePush.php 复制了 PHP 代码,并将其与完成此操作的其余代码一起粘贴。然后我将我的 ck.pem 上传到在线的同一目录中。但是,推送永远不会被传递。这是完整的php:
<?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);
?>
和这条线有关系吗?
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
它在本地时一切正常,但是将 php 与我的 ck.pem 一起在线并运行脚本无法正常工作。
更多信息:
在代码中,当我运行 PHP 时,我只有一个 POST 命令。是这个问题吗,自从我发帖以来,其余的内容都没有运行吗?
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);