php - 用于 iphone 应用程序的 PHP 中的通知 cron

标签 php ios cron apple-push-notifications

我可以通过以下代码手动发送通知。

<?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 集成。但是因为有太多文件。对于我来说,只有一个文件有用。

现在我想通过我的 API 在 iOS 中发生某些事件时运行此 php 脚本。所以我可以在某些函数下编写这段代码,并在某些事件发生时调用该函数。那么最好的方法是什么?以及如何实现每 5 分钟运行一次的通知 cron?

我们将不胜感激。

最佳答案

/etc/cron.d/ 中创建一个 cron 条目

 */5 * * * * root cd /path_to_your_script/ && php your_script.php >> /var/some.log &2>&1

关于php - 用于 iphone 应用程序的 PHP 中的通知 cron,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17131369/

相关文章:

ios - UIImage 通过选择另一个 UIImage 的一部分

php - 无法使用 crontab 运行 shell 脚本

linux - cron执行错误

cron - 如何让 grunt 每天自动运行?

php - Javascript 数组通过 Ajax 传递给 PHP

php - Paypal 成功页面不返回任何试用期订阅数据

php - 将数据传递到 json/php Web 服务

用于复制 MySQL 数据库(包括其 View )的 PHP 脚本?

c# - Xamarin Forms IOS - MKUserTrackingButton 位置

ios - NSString* 到 CFStringRef 导致无用的字符数组