Fix FCM token registration and add queue reminder alerts
- Fix silent insert failure in FCM token registration (missing NOT NULL refresh_token column) so WebView app tokens are actually stored - Add 1-minute queue reminder cron that re-sends FCM alerts for calls still waiting, with transient-based throttle to prevent duplicates - Send FCM cancel on queue dequeue (answered/hangup/timeout), not just on final call status webhook - Clean up new cron hook on plugin deactivation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -577,6 +577,69 @@ class TWP_Call_Queue {
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cron callback: re-send FCM queue alerts every minute for calls still waiting.
|
||||
* Only alerts for calls that have been waiting > 60 seconds (initial alert
|
||||
* already sent on entry). Skips re-alerting for the same call within 55 seconds
|
||||
* using a short transient to avoid overlap with the 60-second cron.
|
||||
*/
|
||||
public static function send_queue_reminders() {
|
||||
global $wpdb;
|
||||
$calls_table = $wpdb->prefix . 'twp_queued_calls';
|
||||
$queue_table = $wpdb->prefix . 'twp_call_queues';
|
||||
|
||||
// Find calls waiting longer than 60 seconds
|
||||
$waiting_calls = $wpdb->get_results(
|
||||
"SELECT c.*, q.queue_name, q.user_id AS queue_owner_id, q.agent_group_id
|
||||
FROM $calls_table c
|
||||
JOIN $queue_table q ON q.id = c.queue_id
|
||||
WHERE c.status = 'waiting'
|
||||
AND c.joined_at <= DATE_SUB(NOW(), INTERVAL 60 SECOND)"
|
||||
);
|
||||
|
||||
if (empty($waiting_calls)) {
|
||||
return;
|
||||
}
|
||||
|
||||
require_once dirname(__FILE__) . '/class-twp-fcm.php';
|
||||
$fcm = new TWP_FCM();
|
||||
|
||||
foreach ($waiting_calls as $call) {
|
||||
// Throttle: skip if we reminded for this call within the last 55 seconds
|
||||
$transient_key = 'twp_queue_remind_' . $call->call_sid;
|
||||
if (get_transient($transient_key)) {
|
||||
continue;
|
||||
}
|
||||
set_transient($transient_key, 1, 55);
|
||||
|
||||
$waiting_minutes = max(1, round((time() - strtotime($call->joined_at)) / 60));
|
||||
$title = 'Call Still Waiting';
|
||||
$body = "Call from {$call->from_number} waiting {$waiting_minutes}m in {$call->queue_name}";
|
||||
|
||||
$notified_users = array();
|
||||
|
||||
// Notify queue owner
|
||||
if (!empty($call->queue_owner_id)) {
|
||||
$fcm->notify_queue_alert($call->queue_owner_id, $call->from_number, $call->queue_name, $call->call_sid);
|
||||
$notified_users[] = $call->queue_owner_id;
|
||||
}
|
||||
|
||||
// Notify agent group members
|
||||
if (!empty($call->agent_group_id)) {
|
||||
require_once dirname(__FILE__) . '/class-twp-agent-groups.php';
|
||||
$members = TWP_Agent_Groups::get_group_members($call->agent_group_id);
|
||||
foreach ($members as $member) {
|
||||
if (!in_array($member->user_id, $notified_users)) {
|
||||
$fcm->notify_queue_alert($member->user_id, $call->from_number, $call->queue_name, $call->call_sid);
|
||||
$notified_users[] = $member->user_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error_log("TWP Queue Reminder: Re-alerted " . count($notified_users) . " user(s) for call {$call->call_sid} waiting {$waiting_minutes}m");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify agents via SMS when a call enters the queue
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user