我使用Ray Wenderlich的tutorial来帮助我。我运行了所有的设置,并使用他包含的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);
答案 0 :(得分:0)
此问题与代码问题无关,这是运行PHP脚本的服务器配置的问题。你需要启用一个&#39; out&#39;端口2195
(以及2196
的反馈)规则适用于防火墙上的Apple服务器。