Entile – MVVM and Extra Information 2

In my last post I did the easy and demo friendly 5-minute install of Entile. But what if you need some more control? Perhaps you’re using the MVVM pattern (actually, I really hope you do), and most likely you need to be able to pass some more information about your user in order to send them relevant toasts and tiles.

Today I thought I’d take you through the things you need to know in order to get Entile working with this scenario – sending and using extra information to the server from an MVVM-based phone project. If you are interested in Windows Phone 7 development but have no idea what I’m talking about, start off by having a look at the Entile summary page.

Client side

Let’s begin with your ViewModel. The main entrypoint for all things Entile on the client is, lo and behold, the EntileClient class. This should be a singleton somewhere in your Windows Phone application. It is pretty much up to you where you want to store it – the important thing is that you need to be able to access it whenever you want to change the status of your registration with the Entile Service (such as turning on/off notifications and passing extra information). In this reasonably simple case I only have a single ViewModel called MainViewModel, so I’ll add the Entile client directly to it:

public class MainViewModel : INotifyPropertyChanged
{
    private readonly EntileClient _entile;

    public MainViewModel()
    {
        _entile = new EntileClient();
    }

    public EntileClient Entile
    {
        get { return _entile; }
    }

Next, let’s drop the EntileView user control on the MainPage (which is the view that uses the MainViewModel. While we’re at it, let’s also add a simple text box where we can enter some extra information to send to the server:

    <StackPanel>
        <TextBlock Text="Enter tile title"/>
        <TextBox Text="{Binding TileTitle, Mode=TwoWay}"/>
                    
        <TestApp:EntileView DataContext="{Binding Entile}"/>
    </StackPanel>

Last thing to do on the client side is to setup the TileTitle property so that it actually passes the information to Entile. This is done by the UpdateExtraInfo method on the EntileClient object.

    private string _tileTitle;
    public string TileTitle
    {
        get { return _tileTitle; }
        set
        {
            _tileTitle = value;
            NotifyPropertyChanged("TileTitle");
            UpdateRegistrationInfo();
        }
    }

    private void UpdateRegistrationInfo()
    {
        var info = new Dictionary<string, string>();
        info["TileTitle"] = TileTitle;
        _entile.UpdateExtraInfo(info);
    }

Running this without modifying anything on the server side will save that extra info in whatever storage you are using (most likely an Xml-file in the App_Data directory).

Server side

OK, to the server side implementation!

As you may have noticed, for each module on the server you generally have two main classes; the Module and the Service. The Module will be instantiated when your application starts up and will be kept in memory until it shuts down. The Service will be created on demand when one of its endpoints is invoked.

You have access to two important interfaces, INotificationQueue and IRegistrator. They are passed on the constructor of the Service or in the Initialize method of the Module.

IRegistrator has methods to query the registration data store for information about any registered clients. You can also subscribe to events such as ClientRegistered or ClientExtraInfoUpdated.

INotificationQueue is the interface that helps you send Tiles and Toasts. The only method you should use is EnqueueItem which takes either a ToastNotification or a TileNotification (and optionally a DateTime if you want to send the notification at a later point in time).

In this example, I’m going to subscribe to the ClientExtraInfoUpdated event and when that happens, immediately send a TileNotification where the Title has been set to whatever the client passed in as the TileTitle in its extra info.

    private INotificationQueue _notificationQueue;
    private IRegistrator _registrator;

    public void Initialize(INotificationQueue notificationQueue, IRegistrator registrator)
    {
        _registrator = registrator;
        _notificationQueue = notificationQueue;
            
        _registrator.ClientExtraInfoUpdated += (a, e) => SendTile(e.UniqueId);
    }

    private void SendTile(string uniqueId)
    {
        var extraInfo = _registrator.GetExtraInfo(uniqueId);
        string tileTitle;
        if (extraInfo.TryGetValue("TileTitle", out tileTitle))
        {
            var tile = new TileNotification(uniqueId)
                            {
                                Title = tileTitle
                            };
                
            _notificationQueue.EnqueueItem(tile);
        }
    }

OK, I was going to add some nice screenshots of this in action, but for some weird reason my WordPress installation doesn’t want me to do that today…

Conclusion

So, this post introduced you to the concept of Extra Information passed from the Entile client to the server. First I showed how you can store the EntileClient object in your MVVM model. I then showed you the UpdateExtraInfo method and how to call it. Finally, I introduced you to the two main server-side interfaces, INotificationQueue and IRegistrator and one way of putting them to use.

As always I welcome all comments, suggestions and, well.. any life-signs at all actually..!

2 thoughts on “Entile – MVVM and Extra Information

  1. Pingback: Tweets that mention Entile – MVVM and Extra Information « Coding Insomnia -- Topsy.com

  2. Depechie Apr 1,2011 00:34

    Can the entile nuget also provide RAW push notifications?

Comments are closed.