0
votes

I'm trying to get a raw push notification to work from Azure Mobile Services to Windows Phone 8.

I've only signed up with Windows Azure for the free mobile services which comes with the free 20mb database and free mobile services.

The site to manage Windows Azure services has a link to an example of how to send a push notification to an app to update a flip tile which can be found here.

On insert into a table a script runs which sends the notification.

There's another example on MSDN which provides an example of how to create an ASP page that sends a raw notification to a WP8 app. That example is here.

I've gotten both examples to work but I need the first example to send a raw notification instead so the code in the second example works.

This is the code I have:

In my Windows Phone 8 app I have this to receive notifications, in App.xaml.cs:

private void AcquirePushChannel()
    {

        /// Holds the push channel that is created or found.
        HttpNotificationChannel pushChannel;

        // The name of our push channel.
        string channelName = "RawSampleChannel";

        // Try to find the push channel.
        pushChannel = HttpNotificationChannel.Find(channelName);

        if (pushChannel == null)
        {
            pushChannel = new HttpNotificationChannel(channelName);

            // Register for all the events before attempting to open the channel.
            pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
            pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
            pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);

            pushChannel.Open();

        }
        else
        {
            // The channel was already open, so just register for all the events.
            pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
            pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
            pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);

            // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
            System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
            //MessageBox.Show(String.Format("Channel Uri is {0}",
             //   pushChannel.ChannelUri.ToString()));

        }

    }

    void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {

        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            // Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
            System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
            MessageBox.Show(String.Format("Channel Uri is {0}",
                e.ChannelUri.ToString()));

        });
    }

    void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
        // Error handling logic for your particular application would be here.
        Deployment.Current.Dispatcher.BeginInvoke(() =>
            MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}",
                e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
                );
    }

    /// <summary>
    /// Event handler for when a raw notification arrives.  For this sample, the raw 
    /// data is simply displayed in a MessageBox.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
    {
        string message;

        using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
        {
            message = reader.ReadToEnd();
        }


        Deployment.Current.Dispatcher.BeginInvoke(() =>
            MessageBox.Show(String.Format("Received Notification {0}:\n{1}",
                DateTime.Now.ToShortTimeString(), message))
                );
    }

In Application Launching it calls AcquirePushChannel:

private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        AcquirePushChannel(); 

    }

My issue is in my Windows Azure Mobile Services database, where I have the following code on insert to a table to send the raw push notification, which doesn't work:

function insert(item, user, request) {

request.execute({
    success: function () {
        // Write to the response and then send the notification in the background
        request.respond();
        // for testing I'm manually putting in the channel ID where it says <channelID> below
        push.mpns.sendRaw(<channelID>, 
            'test', {
            success: function (pushResponse) {
                console.log("Sent push:", pushResponse);
            }
        });
    }
});

}

There is doc on this here, so I'm sure it's correct, but it just doesn't work.

And there's an example here.

One other question is, how can I view console.log via Windows Azure?

1
You can get to the console.log entries on the last option (Logs) of the Azure Mobile Services dashboardJim O'Neil
Thanks heaps for that. I've been able to find out a few things.doktorg

1 Answers

0
votes

I was able to find out from the logs that my code wasn't sending the notification and worked out that it was my method of testing which was causing it and so I've fixed it. I've found out that the insert script only fires when I use the code:

private MobileServiceCollection<TodoItem, TodoItem> items;

private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>();

private async void InsertTodoItem(TodoItem todoItem)
    {
        // This code inserts a new TodoItem into the database. When the operation completes
        // and Mobile Services has assigned an Id, the item is added to the CollectionView
        await todoTable.InsertAsync(todoItem);
        items.Add(todoItem);
    }

The insert script for example doesn't run if you use Management Studio and insert a row manually.