code: private function getTrimmedJson(callable $inner, int $limit, string $error): array
      {
          $gcmInner = $inner($this->text);
          $gcmInnerJson = json_encode($gcmInner, JSON_UNESCAPED_UNICODE);
          if (($gcmInnerJsonLength = \strlen($gcmInnerJson)) > $limit) {
              $cut = $gcmInnerJsonLength - $limit;
              //Note that strlen returns the byte length of the string
              if ($this->text && ($textLength = \strlen($this->text)) > $cut && $this->allowTrimming) {
                  $gcmInner = $inner(mb_strcut($this->text, 0, $textLength - $cut - 3, 'utf8').'...');
              } else {
                  throw new MessageTooLongException("$error $gcmInnerJson");
              }
          }
  
          return $gcmInner;
      }
docs: Using a inner function gets the data, and trys again if its too long by trimming the text.
  
  @throws MessageTooLongException