簡體   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