简体   繁体   English

后台脚本在php中发送通知

[英]Background script to send notifications in php

I have a code which needs to send notifications to all users in the database 我有一个代码,需要将通知发送给数据库中的所有用户

function sendPushNotificationToUsers($users, $message)
    {
        $iPhoneUsers = array();
        $androidUsers = array();
        foreach ($users as $user)
        {
            if($user['deviceType'] == "iPhone")
                $iPhoneUsers[] = $user;
            else if($user['deviceType'] == "Android")
                $androidUsers[] = $user;
        }

        sendApplePushNotifications($iPhoneUsers, $message);
        //sendGooglePushNotifications($androidUsers, $message);         

        return true;
    }

Its usually going to take 2-3 hours. 通常需要2-3个小时。 How can i run this code inside a script which can run in background while i navigate other things in php ? 当我在php中导航其他内容时,如何在可以在后台运行的脚本中运行此代码? I run this code from a form. 我从表单运行此代码。

Use the following at the top of your php script: 在您的php脚本顶部使用以下命令:

set_time_limit(0);
ignore_user_abort(true);

The above code ensures that, even if you close your browser/ssh session , the script will run until it finishes or the web server service is restarted. 上面的代码确保即使关闭browser/ssh session ,脚本也将一直运行到完成或重新启动Web服务器服务为止。


set_time_limit 参数或者set_time_limit

Set the number of seconds a script is allowed to run. 设置允许脚本运行的秒数。 If this is reached, the script returns a fatal error. 如果达到此目的,脚本将返回致命错误。 The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini. 默认限制为30秒,如果存在,则为php.ini中定义的max_execution_time值。

When called, set_time_limit() restarts the timeout counter from zero. 调用时,set_time_limit()从零重新启动超时计数器。 In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out. 换句话说,如果超时是默认的30秒,并且在脚本执行后的25秒内执行了诸如set_time_limit(20)之类的调用,则该脚本将在超时之前运行总计45秒。

ignore_user_abort ignore_user_abort

Sets whether a client disconnect should cause a script to be aborted. 设置客户端断开连接是否应导致脚本中止。

When running PHP as a command line script, and the script's tty goes away without the script being terminated then the script will die the next time it tries to write anything, unless value is set to TRUE 当将PHP作为命令行脚本运行时,脚本的tty消失而脚本未终止,则脚本将在下次尝试写入任何内容时死亡,除非将value设置为TRUE。


Perhaps you can run your php from the command line. 也许您可以从命令行运行php。 So the normal page starts a new process that will actually send the notifications. 因此,正常页面启动了一个新过程,该过程实际上将发送通知。

http://php.net/manual/en/features.commandline.php http://php.net/manual/en/features.commandline.php

While other solutions suggested here could work, you will eventually encounter "scaling" issues as your user base gets larger and larger. 尽管此处建议的其他解决方案可能会起作用,但随着用户群的不断扩大,您最终将遇到“扩展”问题。

The suggested approach in these cases is to move the required functionality into a " queue " for background processing. 在这些情况下,建议的方法是将所需的功能移到“ 队列 ”中进行后台处理。 this way it does not hold the main application thread/server process. 这样,它不保存主应用程序线程/服务器进程。 (more about "messages queues" here: https://en.wikipedia.org/wiki/Message_queue ) (有关“消息队列”的更多信息,请访问: https : //en.wikipedia.org/wiki/Message_queue

Using queues means you send a small payload that requires processing (a message) into a buffer, that is maintained by the queue service. 使用队列意味着您将需要处理的少量有效负载(一条消息)发送到缓冲区中,该缓冲区由队列服务维护。 then "workers", which are individual processes, take these messages and process whatever it is that needs processing. 然后作为单独过程的“工人”接收这些消息并处理需要处理的所有内容。 once a single processing work is done, the message is removed for the queue. 完成单个处理工作后,将删除队列中的消息。 This type of implementation allows your add more and more workers to speed up the overall processing of all messages in the queue. 这种类型的实现允许您添加越来越多的工作程序,以加快队列中所有消息的整体处理速度。

If you're using AWS your could look into SQS (simple queue service) https://aws.amazon.com/sqs/ 如果您使用的是AWS,则可以查看SQS(简单队列服务) https://aws.amazon.com/sqs/

Other open source alternatives are RabbitMQ, Gearman or ZeroMQ 其他开源替代品是RabbitMQ,Gearman或ZeroMQ

javascript JavaScript的

<script language="javascript">
function checking()
{
var user=document.getElementById("user").value;
var message=document.getElementById("message").value;
$.ajax({
type: "POST",cache: false,
url: "url_to_php_file",
data: "user="+user+"&message="+message,
success: function(data) {       
if(data==true)
{
//do simting
}else {
//do simting
}
}); 
}}
</script>

form 形成

<input type="text" id="user" name="user" value="">
<input type="text" id="message" name="message" value="">
<input type="submit" value="Submit" onclick="checking();">

**add to PHP ** **添加到PHP **

$users = $_POST['user'];
$message = $_POST['message'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未收到PHP脚本发送的FCM通知 - Not receiving FCM notifications send by PHP script 在没有阻止的情况下将nodejs脚本发送到php中的后台 - Send nodejs script to background in php without blocking 发送数据到php后台线程脚本 - send data to php background thread script 如何使用PHP向用户发送通知? - How to send notifications to an user with PHP? 通过 Ajax 发送帖子以在后台运行 PHP 脚本 - Send a Post via Ajax to run a PHP script in the background 将参数发送到从PHP在后台运行的Python脚本 - Send Parameter to a Python Script Running at Background From PHP 即使在通过我的PHP脚本获得success1之后,也无法在android中发送推送通知 - Not able to send push notifications in android even after getting success1 through my PHP script 这个 php 脚本是否仍可用于使用 APNs 发送测试推送通知? [基于证书的连接] - Can this php script still be used to send test push notifications using APNs? [Certificate Based Connection] Corona SDK(LUA)如何向php脚本发送请求以注册设备以获取通知? - Corona sdk (lua) how to send request to php script for registering a device to get notifications? 如何使用 FCM 使用 php 脚本向多个设备发送推送通知? - How to send push notifications to multiple devices using php script using FCM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM