Custom Internal Notification

i want to send internal notification i tried sending it with $notification_center->add i.e,

this add notification in the database.\

Now how can i dispatch that notification

$notifications_center = Tygh::$app['notifications_center'];

$notifications_center->add([
'user_id' => $notif_data['user_id'],
'title' => __('vendor.notify_admin.custom.title', [
'[customer]' => $notif_data['customer']
]),
'message' => __('vendor.notify_admin.custom.body', [
'[customer]' => $notif_data['customer']
]),
'area' => 'A',
'section' => NotificationsCenter::SECTION_COMMUNICATION,
'tag' => NotificationsCenter::TAG_MESSAGES,
'action_url' => $notif_data['thread_url'],
'language_code' => Registry::get('settings.Appearance.backend_default_language'),

]);

Hello!

i want to send internal notification i tried sending it with $notification_center->add i.e,

this add notification in the database.\

Now how can i dispatch that notification

$notifications_center = Tygh::$app['notifications_center'];

$notifications_center->add([
'user_id' => $notif_data['user_id'],
'title' => __('vendor.notify_admin.custom.title', [
'[customer]' => $notif_data['customer']
]),
'message' => __('vendor.notify_admin.custom.body', [
'[customer]' => $notif_data['customer']
]),
'area' => 'A',
'section' => NotificationsCenter::SECTION_COMMUNICATION,
'tag' => NotificationsCenter::TAG_MESSAGES,
'action_url' => $notif_data['thread_url'],
'language_code' => Registry::get('settings.Appearance.backend_default_language'),

]);

You can check the Product Reviews add-on for the example.

At first you need to create schema of the notification that should be dispatched:

app/addons/product_reviews/schemas/notifications/events.post.php

$schema['product_reviews.new_post'] = [
    'id'        => 'product_reviews.new_post',
    'group'     => 'product_reviews.product_reviews',
    'name'      => [
        'template' => 'product_reviews.event.new_post',
        'params'   => [
        ],
    ],
    'receivers' => [
        UserTypes::ADMIN => [
            InternalTransport::getId() => InternalMessageSchema::create([
                'tag'           => 'product_reviews.new_post',
                'area'          => SiteArea::ADMIN_PANEL,
                'section'       => NotificationsCenter::SECTION_PRODUCTS,
                'action_url'    => DataValue::create('product_review_url'),
                'language_code' => DataValue::create('company.lang_code', CART_LANGUAGE),
                'severity'      => NotificationSeverity::NOTICE,
                'title'         => [
                    'template' => 'product_reviews.event.new_post.title',
                ],
                'message'       => [
                    'template' => 'product_reviews.event.new_post.message',
                    'params'   => [
                        '[product]' => DataValue::create('product_data.product'),
                    ]
                ],
                'data_modifier' => static function (array $data) {
                    if (empty($data['product_review_data']['product_review_id'])) {
                        return $data;
                    }
                return array_merge($data, [
                    'product_review_url' => fn_url(
                        'product_reviews.update?product_review_id=' . $data['product_review_data']['product_review_id'],
                        SiteArea::ADMIN_PANEL
                    )
                ]);
            }
        ]),
    ],
],

];

Then, you can dispatch this one notification:

        /** @var \Tygh\Notifications\Settings\Factory $notification_settings_factory */
        $notification_settings_factory = Tygh::$app['event.notification_settings.factory'];
        $notification_rules = $notification_settings_factory->create($receivers);
    /** @var \Tygh\Notifications\EventDispatcher $event_dispatcher */
    $event_dispatcher = Tygh::$app['event.dispatcher'];
    $event_dispatcher->dispatch(
        'product_reviews.new_post',
        fn_product_reviews_get_data_for_notification((int) $product_review_id, $auth),
        $notification_rules,
        new ProductReviewsEventProvider((int) $product_review_id)
    );

Don't forget to create the language variable with the name defined in the message>template

'message'       => [
    'template' => 'product_reviews.event.new_post.message',

Hope it helps.

1 Like