0

我正在尝试获取从 Azure 移动服务到 Windows Phone 8 的原始推送通知。

我只在 Windows Azure 上注册了免费的移动服务,它带有免费的 20mb 数据库和免费的移动服务。

管理 Windows Azure 服务的站点有一个链接,该链接指向如何将推送通知发送到应用程序以更新翻转磁贴的示例,该链接可在此处找到。

在插入表时,会运行一个发送通知的脚本。

MSDN 上还有另一个示例,它提供了如何创建向 WP8 应用程序发送原始通知的 ASP 页面的示例。这个例子是here

我已经让两个示例都可以工作,但我需要第一个示例来发送原始通知,以便第二个示例中的代码有效。

这是我的代码:

在我的 Windows Phone 8 应用程序中,我可以在 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))
                );
    }

在应用程序启动中,它调用 AcquirePushChannel:

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

    }

我的问题出在我的 Windows Azure 移动服务数据库中,我在其中插入了以下代码以发送原始推送通知,但这不起作用:

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);
            }
        });
    }
});

}

这里有关于这个的文档,所以我确定它是正确的,但它只是不起作用。

这里有一个例子。

另一个问题是,如何通过 Windows Azure 查看 console.log?

4

1 回答 1

0

我能够从日志中发现我的代码没有发送通知,并确定是我的测试方法导致了它,所以我已经修复了它。我发现插入脚本仅在我使用代码时触发:

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);
    }

例如,如果您使用 Management Studio 并手动插入行,则插入脚本不会运行。

于 2013-08-26T03:23:10.667 回答