2

I am trying to use the getItem and setItem method to display notifications in an array only once.

It works for the first 5 notifications but once you go above 5 there are a few issues that arise. For instance if there are 7 notifications, the first 5 will not repeat as said and as expected but the getItem and setItem method works for the 6th notification in the array and doesn't display the 7th notification.

But then, when the function runs again after 30 seconds, the 7th notification is displayed and the 6th one no longer displays and it keeps fluctuating like that.

I am just trying to get it to display the latest notification every time and once it has displayed it once then don't display it again.

Here is the function:

function show_notifications(){

var command = "FETCH_NOTIFICATIONS";
var user_id = "<?php echo $my_user_id; ?>";

var alerted = localStorage.getItem('alerted');
var alerted0 = localStorage.getItem('alerted0') || '';
var alerted1 = localStorage.getItem('alerted1') || '';
var alerted2 = localStorage.getItem('alerted2') || '';
var alerted3 = localStorage.getItem('alerted3') || '';
var alerted4 = localStorage.getItem('alerted4') || '';

$( document ).ready(function() {
    $.ajax({
        url: ajaxurl,
        dataType: 'JSON',
        data: {action: 'update_db_ajax', command: command, user_id: user_id},
        success: function(result) {

            if(result.sql > 0) {
            $(".latest-notifications").remove();
            $("#notification_count").text(result.sql);
        $("#notification_count").removeClass('no-new-notifications');

            for(var i in result) {
            if (i < 5) continue;

            if(typeof result[i] == "undefined") {
            } else {
            if(result[i].is_viewed === 'NO') {

                    console.log(alerted);

                    if (alerted != result[i].id) {
                        $('#chatAudio')[0].play();

                        iziToast.show({
                            message: result[i].notification_text,
                            messageColor: '#424242',
                            backgroundColor: '#fff',
                            theme: 'light',
                            animateInside: true,
                            layout: 1,
                            close: false,
                            position: 'bottomLeft',
                            timeout: 5000,
                            progressBar: false
                        });

                        localStorage.setItem('alerted', result[i].id);
                    }

                } else if(result[i].is_viewed == 'YES') {
                   $(".latest-notifications-holder").text('No new notifications found.'); 
                }
            }
            }

                if(typeof result[4] == "undefined") {
                } else {
                if(result[4].is_viewed === 'NO') {
                    //console.log(result[4].id);

                    if (alerted4 != result[4].id) {
                        $('#chatAudio')[0].play();

                        iziToast.show({
                            message: result[4].notification_text,
                            messageColor: '#424242',
                            backgroundColor: '#fff',
                            theme: 'light',
                            animateInside: true,
                            layout: 1,
                            close: false,
                            position: 'bottomLeft',
                            timeout: 5000,
                            progressBar: false
                        });

                        localStorage.setItem('alerted4', result[4].id);
                    }
                    $(".latest-notifications-holder").append('<div class="latest-notifications">' + result[4].notification_text + ' <div class="notification-time"><abbr class="timeago" title="' + result[4].creation_date_time +'"></abbr></div> </div>');
                } else if(result[4].is_viewed == 'YES') {
                   $(".latest-notifications-holder").text('No new notifications found.'); 
                } 
                }

                if(typeof result[3] == "undefined") {
                } else {
                if(result[3].is_viewed === 'NO') {
                    //console.log(result[3].id);

                    if (alerted3 != result[3].id) {
                        $('#chatAudio')[0].play();

                        iziToast.show({
                            message: result[3].notification_text,
                            messageColor: '#424242',
                            backgroundColor: '#fff',
                            theme: 'light',
                            animateInside: true,
                            layout: 1,
                            close: false,
                            position: 'bottomLeft',
                            timeout: 5000,
                            progressBar: false
                        });

                        localStorage.setItem('alerted3', result[3].id);
                    }
                    $(".latest-notifications-holder").append('<div class="latest-notifications">' + result[3].notification_text + ' <div class="notification-time"><abbr class="timeago" title="' + result[3].creation_date_time +'"></abbr></div> </div>');
                } else if(result[3].is_viewed == 'YES') {
                   $(".latest-notifications-holder").text('No new notifications found.'); 
                }
                }

                if(typeof result[2] == "undefined") {
                } else {
                if(result[2].is_viewed === 'NO') {
                    //console.log(result[2].id);

                    if (alerted2 != result[2].id) {
                        $('#chatAudio')[0].play();

                        iziToast.show({
                            message: result[2].notification_text,
                            messageColor: '#424242',
                            backgroundColor: '#fff',
                            theme: 'light',
                            animateInside: true,
                            layout: 1,
                            close: false,
                            position: 'bottomLeft',
                            timeout: 5000,
                            progressBar: false
                        });

                        localStorage.setItem('alerted2', result[2].id);
                    }
                    $(".latest-notifications-holder").append('<div class="latest-notifications">' + result[2].notification_text + ' <div class="notification-time"><abbr class="timeago" title="' + result[2].creation_date_time +'"></abbr></div> </div>');
                } else if(result[2].is_viewed == 'YES') {
                   $(".latest-notifications-holder").text('No new notifications found.'); 
                }
                }

                if(typeof result[1] == "undefined") {
                } else {
                if(result[1].is_viewed === 'NO') {
                    //console.log(result[1].id);

                    if (alerted1 != result[1].id) {
                        $('#chatAudio')[0].play();

                        iziToast.show({
                            message: result[1].notification_text,
                            messageColor: '#424242',
                            backgroundColor: '#fff',
                            theme: 'light',
                            animateInside: true,
                            layout: 1,
                            close: false,
                            position: 'bottomLeft',
                            timeout: 5000,
                            progressBar: false
                        });

                        localStorage.setItem('alerted1', result[1].id);
                    }
                    $(".latest-notifications-holder").append('<div class="latest-notifications">' + result[1].notification_text + ' <div class="notification-time"><abbr class="timeago" title="' + result[1].creation_date_time +'"></abbr></div> </div>');
                } else if(result[1].is_viewed == 'YES') {
                   $(".latest-notifications-holder").text('No new notifications found.'); 
                }
                }

                if(typeof result[0] == "undefined") {
                } else {
                if(result[0].is_viewed === 'NO') {
                    //console.log(result[0].id);

                    if (alerted0 != result[0].id) {
                        $('#chatAudio')[0].play();

                        iziToast.show({
                            message: result[0].notification_text,
                            messageColor: '#424242',
                            backgroundColor: '#fff',
                            theme: 'light',
                            animateInside: true,
                            layout: 1,
                            close: false,
                            position: 'bottomLeft',
                            timeout: 5000,
                            progressBar: false
                        });

                        localStorage.setItem('alerted0', result[0].id);
                    }
                    $(".latest-notifications-holder").append('<div class="latest-notifications"><a href="<?php echo $post_url; ?>">' + result[0].notification_text + '</a> <div class="notification-time"><abbr class="timeago" title="' + result[0].creation_date_time +'"></abbr></div> </div>');
                } else if(result[0].is_viewed == 'YES') {
                   $(".latest-notifications-holder").text('No new notifications found.'); 
                }
                }

            } else {
            $("#notification_count").addClass('no-new-notifications');
            }
        },
        complete: function() {
            $('.timeago').timeago();
        }
    });
});
} //function show_notifications

function startTimer(){
//call show_notifications
show_notifications();
//then start interval
setInterval(show_notifications, 30000);
} //function startTimer()

This is the server side php function:

function vba_update($post_id){ 

global $wpdb, $post;

$my_user_id = get_current_user_id();
$post_title = get_the_title( $post_id );
$post_link = get_permalink( $post_id );
$user_company_name = get_the_author_meta('company_name', $my_user_id);
$user_full_name = get_the_author_meta('full_name', $my_user_id);
$user_avatar = get_the_author_meta('avatar', $my_user_id);



$args = array(
    'include'   => tb_get_followers()
            );

$user_query = new WP_User_Query($args);



if (date('I', time())) {
    $creation_date_time = date("Y:m:d H:i:s");
} else {
    $creation_date_time = date("Y-m-d H:i:s", strtotime('+1 hours'));
}

if (date('I', time())) {
    $view_date_time = date("Y:m:d H:i:s");
} else {
    $view_date_time = date("Y-m-d H:i:s", strtotime('+1 hours'));
}


if(!empty($user_company_name)) {
$message = '<a href="'.$post_link.'"><div class="notification-user-image" style="background-image: url('. $user_avatar .');"></div> <strong>' .  $user_company_name . '</strong> posted a new ad <strong>' . substr($post_title, 0, 12).'...' . '</strong></a> <div class="notification-time"><abbr class="timeago" title="' . $creation_date_time . '"></abbr></div>';
} else {
$message = '<a href="'.$post_link.'"><div class="notification-user-image" style="background-image: url('. $user_avatar .');"></div> <strong>' .  $user_full_name . '</strong> posted a new ad <strong>' . substr($post_title, 0, 12).'...' . '</strong></a> <div class="notification-time"><abbr class="timeago" title="' . $creation_date_time . '"></abbr></div>';
}

$notification_text = $message;

if(!empty($user_query->results)) : 

    foreach($user_query->results as $user) :
    $count = $wpdb->get_var( "SELECT COUNT(*) FROM notifications" );

    $id = $count + 1;


    $wpdb->insert(
    'notifications', 
    array(
    'id' => $id,
    'creation_date_time' => $creation_date_time,
    'view_date_time' => $view_date_time,
    'user_id' => $user->ID,
    'notification_text' => $notification_text,
    'is_viewed' => 'NO'
        ),
        array( '%s', '%s', '%s', '%s', '%s', '%s' )
    );
    endforeach;

    endif;

}
  • Is there a particular reason you use 'YES' and 'NO' instead of true or false? How do you tell the server end that it's been viewed? And where is the code for your AJAX endpoint? – Tom J Nowell Jul 14 '18 at 11:42
  • @TomJNowell I am using the admin ajax and I cannot just simply use yes or no or true or false because it's pulling the results from an array and I don't know how to pull each value individually from the array. If I could figure that out I think that would fix the issue. – Matt H Jul 14 '18 at 11:50
  • @TomJNowell I edited the question....please have a look. – Matt H Jul 14 '18 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.