Beispiel #1
0
        /// <summary>
        /// Sends a request to the push server to send a test push notification to the device.
        /// </summary>
        /// <returns>True on success.</returns>
        public async Task <bool> RequestTestPushMessageAsync()
        {
            Logger.Info("Requesting a test push notification...");
            using (TcpConnection connection = new TcpConnection(GetServerAddress(), GetServerPort()))
            {
                try
                {
                    await connection.ConnectAsync();

                    RequestTestPushMessage msg = new RequestTestPushMessage();
                    await connection.SendAsync(msg.ToString());
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to connect and request a test push message.", e);
                    return(false);
                }
                Logger.Debug("Request for test push send. Waiting for a response...");

                try
                {
                    TcpReadResult result = await connection.ReadAsync();

                    connection.Disconnect();
                    if (result.STATE != TcpReadState.SUCCESS)
                    {
                        Logger.Error("Failed to read a response from the push server: " + result.STATE);
                        return(false);
                    }
                    AbstractMessage message = MessageParser.Parse(result.DATA);
                    if (message is SuccessResultMessage)
                    {
                        Logger.Info("Requested test push successfully.");
                        return(true);
                    }

                    if (message is ErrorResultMessage errMsg)
                    {
                        Logger.Error("Failed request a test push. The server responded: " + errMsg.error);
                    }
                    else
                    {
                        Logger.Error("Failed request a test push. The server responded with an unexpected answer: " + result.DATA);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to read a response from the push server.", e);
                }
                return(false);
            }
        }
Beispiel #2
0
        public async Task InitPushForAccountsAsync()
        {
            using (TcpConnection connection = new TcpConnection(GetServerAddress(), GetServerPort()))
            {
                try
                {
                    await connection.ConnectAsync();

                    List <string> accountIds = new List <string>();
                    // If push is disabled, send an empty list:
                    if (Settings.GetSettingBoolean(SettingsConsts.PUSH_ENABLED))
                    {
                        using (MainDbContext ctx = new MainDbContext())
                        {
                            accountIds = ctx.Accounts.Select(a => Utils.ToAccountId(a.bareJid)).ToList();
                        }
                    }
                    SetPushAccountsMessage msg = new SetPushAccountsMessage(accountIds);
                    await connection.SendAsync(msg.ToString());
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to connect and set push accounts.", e);
                    return;
                }
                Logger.Debug("Set push accounts send. Waiting for a response...");

                try
                {
                    TcpReadResult result = await connection.ReadAsync();

                    connection.Disconnect();
                    if (result.STATE != TcpReadState.SUCCESS)
                    {
                        Logger.Error("Failed to read a response from the push server: " + result.STATE);
                        return;
                    }
                    AbstractMessage message = MessageParser.Parse(result.DATA);
                    if (message is SuccessSetPushAccountsMessage msg)
                    {
                        UpdatePushForAccounts(msg.accounts, msg.pushServerBareJid);
                        Settings.SetSetting(SettingsConsts.PUSH_LAST_ACCOUNT_HASH, GetAccountsHash());
                        Logger.Info("Setting push accounts successful.");
                        return;
                    }

                    if (message is ErrorResultMessage errMsg)
                    {
                        Logger.Error("Failed to set push accounts. The server responded: " + errMsg.error);
                    }
                    else
                    {
                        Logger.Error("Failed to set push accounts. The server responded with an unexpected answer: " + result.DATA);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to read a response from the push server.", e);
                }
            }
        }