【问题标题】:Notification cron in PHP for iOS Application适用于 iOS 应用程序的 PHP 中的通知 cron
【发布时间】:2013-06-15 10:51:53
【问题描述】:

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

<?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?

任何帮助将不胜感激。

【问题讨论】:

    标签: cron push-notification apns-php


    【解决方案1】:

    如果你把你的函数放在一个文件中,你可以通过编辑你的 crontab(从命令行“contab -e”,或编辑 /etc/crontab)在 cron 上执行它,并添加以下行:

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

    确保你的用户有执行文件的权限,否则你可能需要sudo命令(*/5 * * * * sudo php -f /path/to/file.php)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多