Why are there no custom notifications Buddypress?

I created a small plugin where users can subscribe to authors, but for some reason users are not notified when the author publishes a new post. Please help to solve this problem. I checked the logs, it shows that the notification was sent, but the notification itself is not there!

<?php
/*
Plugin Name: Author Subscription and Notification
Description: Allows users to subscribe to authors and receive notifications when authors publish new posts.
Version: 1.3
Author: test
*/

// Hook to create the database table upon plugin activation
register_activation_hook(__FILE__, 'create_subscriptions_table');

function create_subscriptions_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'author_subscriptions';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id bigint(20) NOT NULL AUTO_INCREMENT,
        user_id bigint(20) NOT NULL,
        author_id bigint(20) NOT NULL,
        PRIMARY KEY (id),
        UNIQUE KEY user_author (user_id, author_id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

// Hook to add subscription button in the post content
add_filter('the_content', 'add_author_subscription_button_to_post');

function add_author_subscription_button_to_post($content) {
    if (is_single() && is_user_logged_in()) {
        $author_id = get_the_author_meta('ID');
        $current_user_id = get_current_user_id();
        
        global $wpdb;
        $table_name = $wpdb->prefix . 'author_subscriptions';

        // Check if user is already subscribed
        $is_subscribed = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM $table_name WHERE user_id = %d AND author_id = %d",
            $current_user_id, $author_id
        ));

        $button_text = $is_subscribed ? 'Unsubscribe' : 'Subscribe';
        $button_html = '<button id="subscribe-author" class="subscribe-button" data-author-id="' . esc_attr($author_id) . '">' . esc_html($button_text) . '</button>';
        $button_html .= '<div id="subscription-message"></div>';
        
        $content .= $button_html;
    }
    return $content;
}

// Enqueue the JavaScript file and localize script
add_action('wp_enqueue_scripts', 'enqueue_subscription_script');

function enqueue_subscription_script() {
    if (is_single() && is_user_logged_in()) {
        wp_enqueue_script('subscription-script', plugin_dir_url(__FILE__) . 'subscription.js', array('jquery'), '1.0', true);
        wp_localize_script('subscription-script', 'subscriptionData', array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce'    => wp_create_nonce('subscription_nonce')
        ));
    }
}

// Handle the AJAX request for subscription
add_action('wp_ajax_handle_author_subscription', 'handle_author_subscription');

function handle_author_subscription() {
    check_ajax_referer('subscription_nonce', 'nonce');

    if (is_user_logged_in()) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'author_subscriptions';

        $current_user_id = get_current_user_id();
        $author_id = intval($_POST['author_id']);
        $operation = sanitize_text_field($_POST['operation']);
        
        if ($operation === 'subscribe') {
            $wpdb->insert($table_name, array(
                'user_id' => $current_user_id,
                'author_id' => $author_id
            ));
            wp_send_json_success('Subscribed successfully.');
            error_log("User ID $current_user_id subscribed to author ID $author_id");
        } else if ($operation === 'unsubscribe') {
            $wpdb->delete($table_name, array(
                'user_id' => $current_user_id,
                'author_id' => $author_id
            ));
            wp_send_json_success('Unsubscribed successfully.');
            error_log("User ID $current_user_id unsubscribed from author ID $author_id");
        }
    }
    wp_send_json_error('Failed to update subscription.');
}

// Hook to send notifications when an author publishes a new post
add_action('publish_post', 'notify_subscribers_on_new_post');

function notify_subscribers_on_new_post($post_id) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'author_subscriptions';

    $post = get_post($post_id);
    $author_id = $post->post_author;

    error_log("New post published by author ID: " . $author_id);

    // Get subscribers
    $subscribers = $wpdb->get_results($wpdb->prepare(
        "SELECT user_id FROM $table_name WHERE author_id = %d",
        $author_id
    ));

    error_log("Subscribers found: " . count($subscribers));

    foreach ($subscribers as $subscriber) {
        // Send BuddyPress notification
        bp_notifications_add_notification(array(
            'user_id'           => $subscriber->user_id,
            'item_id'           => $post_id,
            'secondary_item_id' => $author_id,
            'component_name'    => 'buddypress',
            'component_action'  => 'new_post_by_subscribed_author',
            'date_notified'     => bp_core_current_time(),
            'is_new'            => true,
        ));
        error_log("Notification sent to user ID: " . $subscriber->user_id);
    }
}

// Custom notification format
add_filter('bp_notifications_get_notifications_for_user', 'custom_bp_notification_format', 10, 5);

function custom_bp_notification_format($content, $item_id, $secondary_item_id, $action, $component_name) {
    if ($component_name === 'buddypress' && $action === 'new_post_by_subscribed_author') {
        $author_name = get_the_author_meta('display_name', $secondary_item_id);
        $post_title = get_the_title($item_id);
        $post_url = get_permalink($item_id);
        
        $content = sprintf(__('New post by %s: <a href="%s">%s</a>', 'text-domain'), $author_name, $post_url, $post_title);
    }
    
    return $content;
}
?>

Here is the log itself!

[23-May-2024 09:42:15 UTC] a:0:{}
[23-May-2024 09:42:40 UTC] New post published by author ID: 1
[23-May-2024 09:42:40 UTC] Subscribers found: 2
[23-May-2024 09:42:40 UTC] Notification sent to user ID: 1
[23-May-2024 09:42:40 UTC] Notification sent to user ID: 27
[23-May-2024 09:42:43 UTC] a:0:{}

New contributor

Lisaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật