2

次のコードとして手動で通知を送信できます。

<?php

// Device token:
$deviceToken = 'xxxxxx';

$passphrase = 'xxxxx';
$badge = 1;

// Displays alert message here:
$message = 'Match Found!';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert',     '/Users/Documents/iOS_Application_Developement/new/APNSPHP/ApnsPHP-master/ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.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,
'badge' => $badge,
'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);

?>

APNS PHP インテグレーションもありましたが、非常に多くのファイルがあります。私の目的では、有用なファイルは 1 つだけです。

APIを介してiOSで特定の出来事が発生したときに、このphpスクリプトを実行したいと思います。したがって、このコードを何らかの関数の下に記述し、特定の事象が発生したときにその関数を呼び出すことができます。それで、それを行う最良の方法は何ですか?5分ごとに実行される通知cronをどのように達成できますか?

どんな助けでも大歓迎です。

4

1 に答える 1

0

関数を 1 つのファイルに入れると、crontab を編集して (コマンド ラインから "contab -e" を実行するか、/etc/crontab を編集して)、次の行を追加して cron で実行できます。

*/5 * * * * php -f /path/to/file.php

ユーザーがファイルを実行する権限を持っていることを確認してください。そうでない場合は、コマンドを sudo する必要がある場合があります (*/5 * * * * sudo php -f /path/to/file.php)。

于 2014-10-31T17:29:16.887 に答える