Получать уведомления (FCM) от RabbitMQ - PullRequest
0 голосов
/ 24 марта 2020

Я сделал pu sh уведомления с комбинацией FCM, RabbitMQ и PHP.

Скрипт для публикации sh сообщения в RabbitMQ, например:

<?php
include('../config.php');
include('vendor/autoload.php');

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;

$host = 'host';
$port = '5672';
$user = 'user';
$pass = 'pass';
$vhost = 'user';
$exchange = 'subscribers';
$queue = 'santri_subscribers';

$connection = new AMQPStreamConnection($host, $port, $user, $pass, $vhost);
$channel = $connection->channel();

$channel->queue_declare($queue, false, true, false, false);

$channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false);

$channel->queue_bind($queue, $exchange);

$sql_token = "SELECT * FROM santri WHERE token<>'' limit 1000";
$query_token = mysqli_query($conn, $sql_token);

while($data_token = mysqli_fetch_array($query_token)){
    $messageBody = json_encode([
        'token' => $data_token['token'],
        'notif' => $data_token['notif'],
    ]);

    $message = new AMQPMessage($messageBody, [
        'content_type' => 'application/json',
        'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
    ]);

    $channel->basic_publish($message, $exchange);
}
echo 'Finished publishing to queue: ' . $queue . PHP_EOL;
// $messageBody = implode(' ', array_slice($argv, 1));

$channel->close();
$connection->close();
?>

Скрипт для получения уведомлений от RabbitMQ, например:

<?php

include('vendor/autoload.php');
include_once('../config.php');

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;

$host = 'host';
$port = '5672';
$user = 'user';
$pass = 'pass';
$vhost = 'user';
$exchange = 'subscribers';
$queue = 'santri_subscribers';

$connection = new AMQPStreamConnection($host, $port, $user, $pass, $vhost);
$channel = $connection->channel();

$channel->queue_declare($queue, false, true, false, false);

$channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false);

$channel->queue_bind($queue, $exchange);

function sendfcm($to, $data){
    $apiKey = api_key_santri;
    $fields = array( 'to' => $to, 'notification' => $data);

    $headers = array('Authorization: key='.$apiKey, 'Content-Type: application/json');

    $url = "https://fcm.googleapis.com/fcm/send";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if ($result === FALSE) {

        die('Curl failed: ' . curl_error($ch));

    }
    curl_close($ch);
    return json_decode($result, true);
}

function process_message($message){

    $messageBody = json_decode($message->body);
    $to = $messageBody->token;
    $notif= $messageBody->notif;

    sendfcm($to, $notif);

    // file_put_contents(dirname(__DIR__) . '/data/' . $token . '.json', $message->body);
    // file_put_contents(dirname(__DIR__) . '/data/' . $notif . '.json', $message->body);


    $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);

}

$consumerTag = 'local.imac.consumer';

$channel->basic_consume($queue, $consumerTag, false, false, false, false, 'process_message');

/**
 * @param \PhpAmqpLib\Channel\AMQPChannel $channel
 * @param \PhpAmqpLib\Connection\AbstractConnection $connection
 */
function shutdown($channel, $connection)
{
    $channel->close();
    $connection->close();
}

register_shutdown_function('shutdown', $channel, $connection);

// Loop as long as the channel has callbacks registered
while ($channel ->is_consuming()) {
    $channel->wait();
}
?>

Чтобы опубликовать sh сообщение для RabbitMQ было успешным, но получить уведомление от RabbitMQ не удалось успешно, я не получил уведомление на моем смартфоне.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...