IEnumerator DeliverMessageCoroutine()

in messaging/testapp/Assets/Firebase/Sample/Messaging/UIHandlerAutomated.cs [227:314]


    IEnumerator DeliverMessageCoroutine(UnityWebRequest request, TaskCompletionSource<string> tcs) {
      yield return request.Send();

#if UNITY_5
    if (request.isError) {
#else
      // After Unity 2017, the UnityWebRequest API changed isError property to isNetworkError for
      // system errors, while isHttpError and responseCode is used for server return code such as
      // 404/Not Found and 500/Internal Server Error.
      if (request.isNetworkError) {
#endif
        DebugLog("The server responded with an error: " + request.error);
        tcs.TrySetException(new Exception(request.error));
      }

      DebugLog("Server response code: " + request.responseCode.ToString());
      DebugLog("Server response contents: " + request.downloadHandler.text);

      // Extract message ID from server response. Unfortunately, there are 3 possible response
      // formats.
      var messageIdCaptureGroup = "([0-9a-f:%]+)";
      // JSON format
      var messageIdMatch = Regex.Match(request.downloadHandler.text, "\"message_id\":\"" +
          messageIdCaptureGroup + "\"");
      // When sending to a topic, a different response format is used, try that.
      if (!messageIdMatch.Success) {
        messageIdMatch = Regex.Match(request.downloadHandler.text, "\"message_id\":" +
          messageIdCaptureGroup);
      }
      if (!messageIdMatch.Success) {
        // Try plaintext format
        messageIdMatch = Regex.Match(request.downloadHandler.text, "id=" + messageIdCaptureGroup);
      }
      if (messageIdMatch.Success) {
        tcs.TrySetResult(messageIdMatch.Groups[1].Value);
      } else {
        tcs.TrySetException(new Exception("Server response doesn't contain message id: " +
              request.downloadHandler.text));
      }
    }

    void ValidatePlaintextMessage(TaskCompletionSource<string> tcs, FirebaseMessage message) {
      try {
        ValidateMessageData(message, "foo", MessageFoo);
        ValidateMessageData(message, "bar", MessageBar);
        tcs.SetResult(message.MessageId);
      } catch (Exception e) {
        tcs.SetException(e);
      }
    }

    void ValidateJsonMessageA(TaskCompletionSource<string> tcs, FirebaseMessage message) {
      try {
        ValidateMessageData(message, "spam", MessageSpam);
        ValidateMessageData(message, "eggs", MessageEggs);
        ValidateMessageNotification(message, MessageNotificationTitle, MessageNotificationBody);
        tcs.SetResult(message.MessageId);
      } catch (Exception e) {
        tcs.SetException(e);
      }
    }

    void ValidateJsonMessageB(TaskCompletionSource<string> tcs, FirebaseMessage message) {
      try {
        ValidateMessageData(message, "foo", MessageFoo);
        ValidateMessageData(message, "bar", MessageBar);
        ValidateMessageNotification(message, MessageNotificationTitle, MessageNotificationBody);
        tcs.SetResult(message.MessageId);
      } catch (Exception e) {
        tcs.SetException(e);
      }
    }

    void ValidateMessageData(FirebaseMessage message, string key, string expectedValue) {
      if (message.Data == null || !message.Data.ContainsKey(key) ||
          message.Data[key] != expectedValue) {
        throw new Exception("Received message doesn't contain expected data");
      }
    }

    void ValidateMessageNotification(FirebaseMessage message, string expectedTitle,
        string expectedBody) {
      if (message.Notification == null || message.Notification.Title != expectedTitle ||
          MessageNotificationBody != expectedBody) {
        throw new Exception("Received message doesn't contain expected notification");
      }
    }
  }