繁体   English   中英

带有Windows Azure移动服务的Windows Phone 8原始通知

[英]Windows Phone 8 Raw Notification with Windows Azure Mobile Services

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

我只向Windows Azure注册了免费的移动服务,该服务随附了免费的20mb数据库和免费的移动服务。

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

在插入表中时,将运行一个脚本来发送通知。

MSDN上还有另一个示例,其中提供了有关如何创建将原始通知发送到WP8应用程序的ASP页的示例。 那个例子在这里

我已经使用了两个示例,但是我需要第一个示例发送原始通知,因此第二个示例中的代码可以工作。

这是我的代码:

在我的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?

我能够从日志中发现我的代码没有发送通知,并确定这是我的测试方法引起的,因此我已对其进行了修复。 我发现插入脚本仅在使用代码时触发:

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并手动插入一行,则插入脚本不会运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM