private IAsyncOperation <ChannelAndWebResponse> ExecuteChannelOperation(IAsyncOperation <PushNotificationChannel> channelOperation /*, String url, String itemId, bool isPrimaryTile*/) { return(channelOperation.AsTask().ContinueWith <ChannelAndWebResponse>((Task <PushNotificationChannel> channelTask) => { PushNotificationChannel newChannel = channelTask.Result; String webResponse = "URI already uploaded"; // Upload the channel URI if the client hasn't recorded sending the same uri to the server //UrlData dataForItem = TryGetUrlData(itemId); var regId = TextSecurePreferences.getWnsRegistrationId(); Debug.WriteLine($"Old Push ID: {regId}"); Debug.WriteLine($"new Push ID: {newChannel.Uri}"); if (regId == null || newChannel.Uri != regId) { bool success = false; Debug.WriteLine("updating"); try { var account = App.Current.accountManager; success = account.setWnsId(newChannel.Uri).Result; success = account.setWnsId(May <string> .NoValue).Result; } catch (Exception e) { Debug.WriteLine(e.Message); } if (success) { UpdateId(newChannel.Uri); } /*HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); * webRequest.Method = "POST"; * webRequest.ContentType = "application/x-www-form-urlencoded"; * byte[] channelUriInBytes = Encoding.UTF8.GetBytes("ChannelUri=" + WebUtility.UrlEncode(newChannel.Uri) + "&ItemId=" + WebUtility.UrlEncode(itemId)); * * Task<Stream> requestTask = webRequest.GetRequestStream(); * using (Stream requestStream = requestTask.Result) * { * requestStream.Write(channelUriInBytes, 0, channelUriInBytes.Length); * } * * Task<WebResponse> responseTask = webRequest.GetResponse(); * using (StreamReader requestReader = new StreamReader(responseTask.Result.GetResponseStream())) * { * webResponse = requestReader.ReadToEnd(); * }*/ } // Only update the data on the client if uploading the channel URI succeeds. // If it fails, you may considered setting another AC task, trying again, etc. // OpenChannelAndUpload will throw an exception if upload fails //UpdateUrl(url, newChannel.Uri, itemId, isPrimaryTile); return new ChannelAndWebResponse { Channel = newChannel, WebResponse = webResponse }; }).AsAsyncOperation()); }
public static async Task <bool> refreshDirectory(SignalServiceAccountManager accountManager) { return(await refreshDirectory(accountManager, TextSecurePreferences.getLocalNumber())); }
/*private UrlData TryGetUrlData(String key) * { * UrlData returnedData = null; * lock (this.urls) * { * if (this.urls.ContainsKey(key)) * { * returnedData = this.urls[key]; * } * } * * return returnedData; * } * * private void SetUrlData(String key, UrlData dataToSet) * { * lock (this.urls) * { * this.urls[key] = dataToSet; * } * }*/ // Update the stored target URL /*private void UpdateUrl(String url, String channelUri, String inputItemId, bool isPrimaryTile) * { * String itemId = isPrimaryTile && inputItemId == null ? MAIN_APP_TILE_KEY : inputItemId; * * bool shouldSerializeTileIds = TryGetUrlData(itemId) == null; * UrlData storedData = new UrlData() { Url = url, ChannelUri = channelUri, IsAppId = isPrimaryTile, Renewed = DateTime.Now }; * SetUrlData(itemId, storedData); * * using (MemoryStream stream = new MemoryStream()) * { * DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UrlData)); * serializer.WriteObject(stream, storedData); * stream.Position = 0; * using (StreamReader reader = new StreamReader(stream)) * { * ApplicationData.Current.LocalSettings.Values[itemId] = reader.ReadToEnd(); * } * } * * if (shouldSerializeTileIds) * { * SaveAppTileIds(); * } * }*/ /*private void SaveAppTileIds() * { * List<String> dataToStore; * * lock (this.urls) * { * dataToStore = new List<String>(this.urls.Count); * foreach (String key in this.urls.Keys) * { * dataToStore.Add(key); * } * } * * using (MemoryStream stream = new MemoryStream()) * { * DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<String>)); * serializer.WriteObject(stream, dataToStore); * stream.Position = 0; * using (StreamReader reader = new StreamReader(stream)) * { * ApplicationData.Current.LocalSettings.Values[APP_TILE_ID_KEY] = reader.ReadToEnd(); * } * } * }*/ private void UpdateId(string id) { TextSecurePreferences.setWnsRegistrationId(id); }