firebase / firebase-admin-dotnet

Firebase Admin .NET SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
369 stars 131 forks source link

Sending notification from WCF in .NET Framework 4.8 is stuck #366

Open Thordax opened 12 months ago

Thordax commented 12 months ago

I have a WCF (Windows Communication Foundation) server. Recently, I tried to update the sending of notifications from a basic https request to Firebase Admin SDK.

The code I'm using in console version works perfectly from my machine. On the other hand, the same code used from the WCF on the same machine remains blocked when sending.

Here's the method I use:

public static async Task<string> SendMessage(string deviceId, string message, string IdSem, string UrlResponseSEM,
    string Auth_Message = "", string auth_key = "", bool isAuthMessageDansNotif = true)
{
    string color = "#0072C6";
    bool hasErrors = false;
    List<string> logList = new List<string> { "[=FCM_SendMessage=]" };

    try
    {
        logList.Add($"[deviceId:{deviceId}] [message:{message}] [IdSem:{IdSem}] [Auth_Message:{Auth_Message}] [color:{color}]");
        logList.Add($"[isAuthMessageDansNotif:{isAuthMessageDansNotif}]");

        Message messagingMessage = null;
        if (isAuthMessageDansNotif)
        {
            messagingMessage = new Message
            {
                Token = deviceId,
                Data = new Dictionary<string, string>
                {
                    { "time", DateTime.Now.ToString() },
                    { "idsem", IdSem },
                    { "urlresponse", UrlResponseSEM },
                    { "message", message },
                    { "auth_message", Auth_Message },
                    { "auth_key", auth_key }
                }
            };
        }
        else
        {
            messagingMessage = new Message
            {
                Token = deviceId,
                Data = new Dictionary<string, string>
                {
                    { "time", DateTime.Now.ToString() },
                    { "idsem", IdSem },
                    { "urlresponse", UrlResponseSEM },
                    { "message", message }
                }
            };
        }

        string postData = JsonConvert.SerializeObject(messagingMessage);
        logList.Add($"[postData={postData}]");

        LogSeo.MyLog.Debug("FCM_SENDING_MESSAGE ...");

        var response = await FirebaseMessaging.DefaultInstance.SendAsync(messagingMessage).ConfigureAwait(false);
        LogSeo.MyLog.Debug("FCM_SENDING_MESSAGE OK");

        return response;
    }
    catch (Exception ex)
    {
        hasErrors = true;
        logList.Add($"ERROR > {ex.GetAllExceptions()} {ex.StackTrace}");
        return $"ERROR:{ex.Message}";
    }
    finally
    {
        logList.Add("[=FCM_SendMessage=]");
        string strLog = string.Join(Environment.NewLine, logList);
        if (hasErrors)
        {
            LogSeo.MyLog.Error(strLog);
        }
        else
        {
            LogSeo.MyLog.Debug(strLog);
        }
    }
}

The method remains blocked at the following line:

var response = await FirebaseMessaging.DefaultInstance.SendAsync(messagingMessage).ConfigureAwait(false);

Could you please tell me what could have caused the problem?

Note that I'm using a proxy and that there may also be a firewall on the machine. Are there any specific ports that need to be unblocked?

If so, how can this work from my executable?

Thanks in advance for your help, and have a nice day,

I tried to send a notification through FCM from my WCF.

google-oss-bot commented 12 months ago

I found a few problems with this issue:

Thordax commented 11 months ago

After many investigations, the problem concerned this code:

options.HttpClientFactory = new ProxyAwareHttpClientFactory();
//...
public class ProxyAwareHttpClientFactory : HttpClientFactory
{
    protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
    {
        var httpClientHandler = new HttpClientHandler
        {
            Proxy = new WebProxy(GlobalSettings.ProxyUrl, GlobalSettings.ProxyPort),
            UseProxy = true
        };
        return httpClientHandler;
    }
}

I thought this code would allow Firebase to send messages via the proxy specified in the ProxyAwareHttpClientFactory. However, this is not the case, the proxy is not used. To get around the problem, I specified the proxy in the web.config file, as follows:

In my web.config :

<system.net>  
    <defaultProxy enabled="true" useDefaultCredentials="true">  
      <proxy  
        usesystemdefault="true"  
        proxyaddress="http://myproxy:8080"  
        bypassonlocal="true"  
      />  
    </defaultProxy>  
  </system.net>

This allows FCM to send requests correctly via the proxy.