Create transports

Hi all. I’m sending a link in our application. Made a transport that provides the function of sending messages through Firebase. But I haven’t yet understood the logic of getting the recipient.
My transport:

<?php

namespace Tygh\Notifications\Transports\Firebase;

use Tygh\Exceptions\DeveloperException;
use Tygh\Notifications\Transports\BaseMessageSchema;
use Tygh\Notifications\Transports\ITransport;

class FirebaseTransports implements ITransport
{
    protected $firebase;

    protected $receiver_finder_factory;

    public function __construct(Firebase $firebase)
    {
        $this->firebase = $firebase;
    }

    public static function getId()
    {
        return 'firebase';
    }

    public function fn_user_fcm_token ($schema, $receivers) {
        $body = array(
            'title'         => $schema->title,
            'data'          => $schema->data,
        );
        $message = array(
            'to'            => $receivers,
            'notifications' => $body,
        );
        return fn_send_notification_firebase(
            $message,
            $receivers
        );
    }

    public function process(BaseMessageSchema $schema, array $receiver_search_conditions)
    {
        if (!$schema instanceof FirebaseMessageSchema) {
            throw new DeveloperException('Input data should be instance of FirebaseMessageSchema');
        }

        $receivers = $this->getReceivers($receiver_search_conditions, $schema);

        return $this->fn_user_fcm_token($schema, $receivers);
    }

    protected function getReceivers(array $receiver_search_conditions, FirebaseMessageSchema $schema)
    {
        $fcmTokens = [];

        foreach ($receiver_search_conditions as $condition) {
            $finder = $this->receiver_finder_factory->get($condition->getMethod());
            $fcmTokens = array_merge($fcmTokens, $finder->find($condition->getCriterion(), $schema));
        }

        $fcmTokens = array_unique($fcmTokens);

        if (!$fcmTokens) {
            $fcmTokens = db_get_array("SELECT token FROM ?:fcm_tokens WHERE user_id IN (?a)", array_keys($receiver_search_conditions));
        }

        $fcmTokens = array_unique(array_column($fcmTokens, 'token'));

        // TODO: Сreate function check fcm token

        return $fcmTokens;
    }
}

Together I made a Firebase diagram.

Events have reached a dead end.
$order_event = [
‘id’ => ‘order.status_changed’,
‘group’ => ‘orders’,
‘name’ => [
‘template’ => ‘event.order.status_changed.name’,
‘params’ => [
‘[status]’ => ‘’,
],
],
‘data_provider’ => [OrderDataProvider::class, ‘factory’],
‘receivers’ => [
UserTypes::CUSTOMER => [
MailTransport::getId() => MailMessageSchema::create([
‘area’ => SiteArea::STOREFRONT,
‘from’ => ‘company_orders_department’,
‘to’ => DataValue::create(‘order_info.email’),
‘template_code’ => DataValue::create(‘template_code’),
‘legacy_template’ => ‘orders/order_notification.tpl’,
‘company_id’ => DataValue::create(‘order_info.company_id’),
‘storefront_id’ => DataValue::create(‘order_info.storefront_id’),
‘language_code’ => DataValue::create(‘order_info.lang_code’, CART_LANGUAGE)
]),
],
UserTypes::ADMIN => [
MailTransport::getId() => MailMessageSchema::create([
‘area’ => SiteArea::ADMIN_PANEL,
‘from’ => ‘default_company_orders_department’,
‘to’ => ‘default_company_orders_department’,
‘reply_to’ => DataValue::create(‘order_info.email’),
‘template_code’ => DataValue::create(‘template_code’),
‘legacy_template’ => ‘orders/order_notification.tpl’,
‘company_id’ => DataValue::create(‘order_info.company_id’),
‘to_company_id’ => DataValue::create(‘order_info.company_id’),
‘storefront_id’ => DataValue::create(‘order_info.storefront_id’),
‘language_code’ => DataValue::create(‘lang_code’, CART_LANGUAGE)
])
],
],
];
From here, as I understand it, I need to pass user_id via:

DataValue::create('order_info.user_id')

And then what should I do to get the user_id in $receiver_search_conditions?

You can check how the $transport->process is being called in the \Tygh\Notifications\EventDispatcher::dispatch() (app/Tygh/Notifications/EventDispatcher.php) and specifically \Tygh\Notifications\EventDispatcher::getReceiverSearchConditions()

Thank you! I’ll take care of it.

Possibly cs-cart team solved your issue. The following may not be of any use, I was trying to understand your code and asked chat GPT, the response included this unrequested trouble shooting tip …

about passing user_id via DataValue::create('order_info.user_id') and then retrieving it in $receiver_search_conditions, it seems like you’re trying to pass user-specific data to customize the message delivery. However, from the provided code, it’s not explicitly clear how user_id is utilized in $receiver_search_conditions.

To better understand how user_id is used in your application, you should examine the code where the $transport->process() method is called, particularly in the \Tygh\Notifications\EventDispatcher::dispatch() method. Additionally, check the implementation of \Tygh\Notifications\EventDispatcher::getReceiverSearchConditions() to understand how receiver search conditions are obtained and whether user_id plays a role in defining these conditions.

1 Like