L
L
LocKing2018-05-23 16:39:46
PHP
LocKing, 2018-05-23 16:39:46

Web PUSH notifications not sent via CRON?

The task is to send Web PUSH notifications to users of the site when a certain event occurs. This must be done as quickly as possible (the delay should not be more than 1 minute). Therefore, I decided not to use all sorts of services that provide such an opportunity, but to use the basic solution through Firebase Cloud Messaging, the instructions for which
are described in detail here The user token is received and stored in the database. Those. permission to receive messages is received, the token is known.
Every minute, cron is launched and executes a php script that, if necessary, sends a message via curl to fcm.googleapis.com/fcm/send (and from there a request is already sent to the browser to display a push notification).

$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $control->_connect_ini['google_url']);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $response = curl_exec($ch);
    curl_close($ch);

Everything is as instructed. However, if you run this script manually from the console, everything goes fine in 100% of cases. If this script is run by cron, then the message is not displayed in 90% of cases, although the entire code works without errors and an array with data on successful sending comes as a response from Google:
{
  "canonical_ids": 0,
  "failure": 0,
  "multicast_id": 5652500584254823832,
  "results": [
    {
        "message_id": "0:1527001372290285%2fd9afcdf9fd7ecd"
    }
  ],
  "success": 1

}

I can't figure out where the dog is buried.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2018-05-23
@402d

because of Roskomnadzor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question