Skip to content

Commit

Permalink
MDL-78072 airnotifier: Apply payload size optimisation to all push
Browse files Browse the repository at this point in the history
Backport of MDL-77893.

Co-authored by: Andrew Lyons <andrew@nicols.co.uk>
  • Loading branch information
jleyva authored and andrewnicols committed May 9, 2023
1 parent 1af4ace commit d838917
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions message/output/airnotifier/message_output_airnotifier.php
Expand Up @@ -133,6 +133,15 @@ public function send_message($eventdata) {

$extra->encrypted = $encryptnotifications;
$extra = $this->encrypt_payload($extra, $devicetoken);

// We use Firebase to deliver all Push Notifications, and for all device types.
// Firebase has a 4KB payload limit.
// https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
// If the message is over that limit we remove unneeded fields and replace the title with a simple message.
if (\core_text::strlen(json_encode($extra), '8bit') > 4000) {
$extra->smallmessage = get_string('view_notification', 'message_airnotifier');
}

$params = array(
'device' => $devicetoken->platform,
'token' => $devicetoken->pushid,
Expand Down Expand Up @@ -203,14 +212,6 @@ protected function encrypt_payload(stdClass $payload, stdClass $devicetoken): st
unset($payload->attachment);
unset($payload->attachname);

// We use Firebase to deliver all Push Notifications, and for all device types.
// Firebase has a 4KB payload limit.
// https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
// If the message is over that limit we remove unneeded fields and replace the title with a simple message.
if (\core_text::strlen(json_encode($payload), '8bit') > 4000) {
$payload->smallmessage = get_string('view_notification', 'message_airnotifier');
}

return $payload;
}

Expand Down

0 comments on commit d838917

Please sign in to comment.