0

Hi I use this plugin for create events https://wordpress.org/plugins/modern-events-calendar-lite/ and this for send notification on my android app. https://github.com/dream-space/wp-fcm

I make modification on plugin to send reminder notification for upcoming events (3days) before. The modification look like like.

`

 function fcm_main_get_option_event(){
  $options = get_option('fcm_event_setting');
  if(!is_array($options)){
    $options = array(
        'event_check' => 0,
    );
 }

return $options;
}

if (!wp_next_scheduled('my_task_hook')) {
wp_schedule_event( time(), 'daily', 'my_task_hook' );
}
add_action ( 'my_task_hook', 'my_task_function' );

`

And function to send notification:

`

function my_task_function() {
$options  = get_option('fcm_event_setting');
 //  $is_send_notif = false;
if(!empty($options['event_check'])) {
   $is_send_notif = true;
  $args = array(  
    'post_type' => 'mec-events',
    'post_status' => 'publish',
    'posts_per_page' => -1, 
  );

$loop = new WP_Query( $args ); 

    
while ( $loop->have_posts() ) : $loop->the_post(); 
$datetime2 = date("Y-m-d"); 
$my_meta = get_post_meta(get_the_ID(), 'mec_start_date', true ); 
$diff = strtotime($my_meta) - strtotime($datetime2);
    $gap = abs(round($diff / 86400)); 

    
    if($gap == '3'){
         $is_send_notif = true;
         $title = 'Потсетување';
         $event = "POTSETNIK 1";
        $body    = 'For '.$gap.' days :' .get_the_title();

    }


    if($is_send_notif == true){
    $message = array(
        'title'     => $title,
        'content'   =>  'For '.$gap.' days:' .get_the_title()

    );

    $total = fcm_data_get_all_count();
    if($total <= 0) return;

    $respon  = fcm_notif_divide_send("", $total, $message);

    fcm_data_insert_log($title, $content, "ALL", $event, $respon['status']);
}
    endwhile;
wp_reset_postdata();
}
}

`

This work, but send notification 3 days before, and 3 days after the event.

Where is the problem.

Thanks?

0

It's because your code is explicitly written to ignore whether or not the event is in the past or the future.

See here:

$diff = strtotime($my_meta) - strtotime($datetime2);

If the event date is in the future, $diff will be a positive number, but if it's in the past, it will be a negative number. Then the very next line is:

$gap = abs(round($diff / 86400));

The whole point of the abs() function is to make sure a number is a positive number. Which means that the result will be exactly the same whether the event is in the past or in the future.

If you only want to send a notification for upcoming events then you need to know if the number is negative. So just remove abs() and make sure that you're only checking for 3 and not -3:

$diff = strtotime($my_meta) - strtotime($datetime2);
$gap = round($diff / 86400); 


if($gap === 3){
    // etc.
}
2
  • Thanks,Let me test.
    – Dimitry
    Mar 18 '21 at 10:29
  • with this changes not send notification. :/
    – Dimitry
    Mar 19 '21 at 17:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.