From 07168482d78ec592766d3be902067f7df3ac0c5a Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Thu, 12 Mar 2026 10:59:06 +0100 Subject: [PATCH] MDL-87795 message_airnotifier: Handle 404 (unregistered token) responses --- .upgradenotes/MDL-87795-2026021809474024.yml | 7 +++++++ .../output/airnotifier/message_output_airnotifier.php | 8 ++++++++ public/user/lib.php | 7 +++++-- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .upgradenotes/MDL-87795-2026021809474024.yml diff --git a/.upgradenotes/MDL-87795-2026021809474024.yml b/.upgradenotes/MDL-87795-2026021809474024.yml new file mode 100644 index 00000000000..f2af11ad37b --- /dev/null +++ b/.upgradenotes/MDL-87795-2026021809474024.yml @@ -0,0 +1,7 @@ +issueNumber: MDL-87795 +notes: + core_user: + - message: >- + Added new optional parameter `userid` to the `user_remove_user_device` + function. + type: improved diff --git a/public/message/output/airnotifier/message_output_airnotifier.php b/public/message/output/airnotifier/message_output_airnotifier.php index e260a44c098..59679b24211 100644 --- a/public/message/output/airnotifier/message_output_airnotifier.php +++ b/public/message/output/airnotifier/message_output_airnotifier.php @@ -160,6 +160,14 @@ class message_output_airnotifier extends message_output { // JSON POST raw body request. $resp = $curl->post($serverurl, json_encode($params)); + + // Check if the device token is no longer registered. + if ($curl->info['http_code'] === 404) { + $json = json_decode($resp, true); + if (($json['error'] ?? null) === 'Unregistered token') { + user_remove_user_device($devicetoken->uuid, $devicetoken->appid, $devicetoken->userid); + } + } } return true; diff --git a/public/user/lib.php b/public/user/lib.php index d724d566e13..0e9dbe2edc1 100644 --- a/public/user/lib.php +++ b/public/user/lib.php @@ -1148,13 +1148,16 @@ function user_is_previously_used_password($userid, $password) { * * @param string $uuid The device UUID. * @param string $appid The app id. If empty all the devices matching the UUID for the user will be removed. + * @param int|null $userid The user id. If null, the current user will be used. * @return bool true if removed, false if the device didn't exists in the database * @since Moodle 2.9 */ -function user_remove_user_device($uuid, $appid = "") { +function user_remove_user_device($uuid, $appid = "", $userid = null) { global $DB, $USER; - $conditions = array('uuid' => $uuid, 'userid' => $USER->id); + $userid ??= $USER->id; + + $conditions = ['uuid' => $uuid, 'userid' => $userid]; if (!empty($appid)) { $conditions['appid'] = $appid; }