Skip to content

Commit

Permalink
Merge branch 'master' into publishWebSite
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Sep 9, 2013
2 parents 3c7e0aa + d20548c commit b030e01
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/IronPigeon.Relay/Controllers/AddressBookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ActionResult Index(string blob) {
Requires.NotNullOrEmpty(blob, "blob");

var blobUri = new Uri(blob, UriKind.Absolute);
if (!this.Request.AcceptTypes.Contains(AddressBookEntry.ContentType) && this.Request.AcceptTypes.Contains("text/html")) {
if (this.Request.AcceptTypes != null && !this.Request.AcceptTypes.Contains(AddressBookEntry.ContentType) && this.Request.AcceptTypes.Contains("text/html")) {
// This looks like a browser rather than an IronPigeon client.
// Return an HTML page that describes what IronPigeon is.
return this.View();
Expand Down
22 changes: 20 additions & 2 deletions src/IronPigeon.Relay/Controllers/InboxController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public async Task<JsonResult> CreateAsync() {
public async Task<ActionResult> GetInboxItemsAsync(string id, bool longPoll = false) {
var blobs = await this.RetrieveInboxItemsAsync(id, longPoll);
var list = new IncomingList() { Items = blobs };

// Unit tests may not set this.Response
if (this.Response != null) {
// Help prevent clients such as WP8 from caching the result since they operate on it, then call us again
Expand Down Expand Up @@ -230,6 +230,8 @@ public async Task<ActionResult> PushChannelAsync(string id) {

inbox.WinPhone8PushChannelUri = channelUri.AbsoluteUri;
inbox.WinPhone8PushChannelContent = content;
inbox.WinPhone8ToastText1 = this.Request.Form["wp8_channel_toast_text1"];
inbox.WinPhone8ToastText2 = this.Request.Form["wp8_channel_toast_text2"];
} else {
// No data was posted. So skip updating the entity.
return new EmptyResult();
Expand Down Expand Up @@ -409,14 +411,30 @@ private async Task PushNotifyInboxMessageWinPhoneAsync(InboxEntity inbox) {
int count = await this.RetrieveInboxItemsCountAsync(inbox.RowKey);
bool invalidChannel = false;
try {
await notifications.PushWinPhoneTileAsync(count: count);
var pushTile = notifications.PushWinPhoneTileAsync(count: count);
Task<bool> pushToast = Task.FromResult(false);
if (!string.IsNullOrEmpty(inbox.WinPhone8ToastText1) || !string.IsNullOrEmpty(inbox.WinPhone8ToastText2)) {
var line1 = string.Format(CultureInfo.InvariantCulture, inbox.WinPhone8ToastText1 ?? string.Empty, count);
var line2 = string.Format(CultureInfo.InvariantCulture, inbox.WinPhone8ToastText2 ?? string.Empty, count);
pushToast = notifications.PushWinPhoneToastAsync(line1, line2);
}

Task<bool> pushRaw = Task.FromResult(false);
if (!string.IsNullOrEmpty(inbox.WinPhone8PushChannelContent)) {
pushRaw = notifications.PushWinPhonRawNotificationAsync(inbox.WinPhone8PushChannelContent);
}

await Task.WhenAll(pushTile, pushToast, pushRaw);
invalidChannel |= !(pushTile.Result || pushToast.Result || pushRaw.Result);
} catch (HttpRequestException) {
invalidChannel = true;
}

if (invalidChannel) {
inbox.WinPhone8PushChannelUri = null;
inbox.WinPhone8PushChannelContent = null;
inbox.WinPhone8ToastText1 = null;
inbox.WinPhone8ToastText2 = null;
this.InboxTable.UpdateObject(inbox);
await this.InboxTable.SaveChangesAsync();
}
Expand Down
10 changes: 10 additions & 0 deletions src/IronPigeon.Relay/Models/InboxEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public InboxEntity() {
/// </summary>
public string WinPhone8PushChannelContent { get; set; }

/// <summary>
/// Gets or sets the first line of text to include in the toast notification when a message comes in.
/// </summary>
public string WinPhone8ToastText1 { get; set; }

/// <summary>
/// Gets or sets the second line of text to include in the toast notification when a message comes in.
/// </summary>
public string WinPhone8ToastText2 { get; set; }

/// <summary>
/// Gets a value indicating whether push notification is enabled.
/// </summary>
Expand Down
10 changes: 8 additions & 2 deletions src/IronPigeon.WinPhone8/WinPhoneChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ public class WinPhoneChannel : Channel {
/// </summary>
/// <param name="pushNotificationChannel">The push notification channel.</param>
/// <param name="pushContent">Content of the push.</param>
/// <param name="toastLine1">The first line in the toast notification to send.</param>
/// <param name="toastLine2">The second line in the toast notification to send.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task representing the async operation.</returns>
public async Task RegisterPushNotificationChannelAsync(HttpNotificationChannel pushNotificationChannel, string pushContent, CancellationToken cancellationToken = default(CancellationToken)) {
/// <returns>
/// A task representing the async operation.
/// </returns>
public async Task RegisterPushNotificationChannelAsync(HttpNotificationChannel pushNotificationChannel, string pushContent = null, string toastLine1 = null, string toastLine2 = null, CancellationToken cancellationToken = default(CancellationToken)) {
Requires.NotNull(pushNotificationChannel, "pushNotificationChannel");

var request = new HttpRequestMessage(HttpMethod.Put, this.Endpoint.PublicEndpoint.MessageReceivingEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.Endpoint.InboxOwnerCode);
request.Content = new FormUrlEncodedContent(new Dictionary<string, string> {
{ "wp8_channel_uri", pushNotificationChannel.ChannelUri.AbsoluteUri },
{ "wp8_channel_content", pushContent ?? string.Empty },
{ "wp8_channel_toast_text1", toastLine1 ?? string.Empty },
{ "wp8_channel_toast_text2", toastLine2 ?? string.Empty },
});
var response = await this.HttpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
Expand Down
18 changes: 14 additions & 4 deletions src/IronPigeon/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,19 @@ public static async Task WithCancellation(this Task task, CancellationToken canc
return default(TOutput);
}

/// <summary>
/// Applies the appropriate headers to an HTTP request so that the response will not be cached.
/// </summary>
/// <param name="request">The request.</param>
public static void ApplyNoCachePolicy(this HttpRequestMessage request) {
Requires.NotNull(request, "request");

// The no-cache headers don't seem to impact the client at all, but perhaps they prevent any intermediaries from caching?
request.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true };
request.Headers.Pragma.Add(new NameValueHeaderValue("no-cache"));
request.Headers.IfModifiedSince = DateTime.UtcNow; // This last one seems to be the trick that actually works.
}

/// <summary>
/// Guesses the hash algorithm used given the length of the result.
/// </summary>
Expand Down Expand Up @@ -512,10 +525,7 @@ internal static Task<HttpResponseMessage> GetAsync(this HttpClient httpClient, U
// which would change the output of a future request to the server with the same URL.
// But if a cached result is used instead of a real request to the server then we get
// the same result back.
// The no-cache headers don't seem to impact the client at all, but perhaps they prevent any intermediaries from caching?
request.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true };
request.Headers.Pragma.Add(new NameValueHeaderValue("no-cache"));
request.Headers.IfModifiedSince = DateTime.UtcNow; // This last one seems to be the trick that actually works.
ApplyNoCachePolicy(request);

return httpClient.SendAsync(request, cancellationToken);
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0.1.8.10
0.1.8.11
-preview

0 comments on commit b030e01

Please sign in to comment.