14.01.2015 Views

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Check Box for the AcceptBroadcast field. Our new UI will be as<br />

follows<br />

Subscribing Channels<br />

We’ll put in a Clicked handler for the Subscribe button and update<br />

the client side code as follows:<br />

- In MainPage.xaml, we add the following statement to be able to<br />

access the new ChannelSub table<br />

As we can see, we have added a new table called ChannelSub and<br />

added the following Insert Script.<br />

function insert(item, user,<br />

request) {<br />

var channelTable = tables.<br />

getTable(‘ChannelSub’);<br />

channelTable<br />

.where({ channel:<br />

item.channel })<br />

.read({ success:<br />

insertChannelIfNotFound });<br />

function insertChannelIfNotFound<br />

(existingChannels) {<br />

if (existingChannels.length ><br />

0) {<br />

request.respond(200, existingChannels[0]);<br />

} else {<br />

request.execute();<br />

}<br />

}<br />

}<br />

private IMobileServiceTable channelSubs =<br />

App.MobileService.GetTable();<br />

- The Subscription button click handler is as follows<br />

private async void SubscribeButton_Click(object sender,<br />

RoutedEventArgs e) {<br />

await channelSubs.InsertAsync(new ChannelSub<br />

{<br />

MyId = MyIdTextBox.Text,<br />

AcceptBroadcast = AcceptBroadcastCheckBox.IsChecked.<br />

Value,<br />

Channel = App.CurrentChannel.Uri<br />

});<br />

}<br />

The script checks the table to see if the requested Item’s Channel<br />

URI already exists, if not, it inserts the new Channel Subscription<br />

requests.<br />

Sending out Push Notifications based on Type of<br />

Subscription<br />

Now that we have subscription information with us, let’s modify<br />

the notification logic so that:<br />

a. Users not subscribed to Broadcasts do not receive notifications<br />

without a To ID and the ones who are subscribed, get all requests<br />

b. Instead of lobbing the Notification back to where it came from,<br />

if it has a To ID, send to that subscription ONLY.<br />

On the server, we’ll have to setup the Insert script such that it<br />

doesn’t allow duplicate subscription requests.<br />

To achieve the above two, we update the code with the Insert<br />

script of the FakeTweetMessage as follows:<br />

function insert(item, user, request) {<br />

request.execute({<br />

success: function() {<br />

request.respond();<br />

sendNotifications();<br />

}<br />

});<br />

function sendNotifications() {<br />

DNcmagazine www.dotnetcurry.com | 79

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!