我試圖在WP7.1中使用推送通知。我創建了類和一些PHP類。該示例適用於所有通知類型,但我的問題是UriChannel。用我的簡單示例,我必須在php代碼中手動編寫URI通道。推送通知,Uri通道和PHP服務器端
對於安裝了我的應用程序的所有設備,uri通道是否是唯一的?
如何將Uri頻道發送到我的服務器?
謝謝。
這是我的代碼:
/// Holds the push channel that is created or found.
HttpNotificationChannel pushChannel;
// The name of our push channel.
string channelName = "LiveTileChannel";
// Costruttore
public MainPage()
{
InitializeComponent();
CreatePushChannel();
}
private void CreatePushChannel()
{
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to the push service.
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.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(pushChannel_HttpNotificationReceived);
pushChannel.Open();
// Bind this new channel for Tile events.
pushChannel.BindToShellTile();
pushChannel.BindToShellToast();
connessioneTxt.Text = "Connessione avvenuta";
}
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.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
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());
uriTxt.Text=pushChannel.ChannelUri.ToString();
connessioneTxt.Text = "Connessione già avvenuta";
}
}
void pushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
string message;
using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
{
message = reader.ReadToEnd();
}
Dispatcher.BeginInvoke(() => rawTxt.Text = String.Format("Received Notification {0}:\n{1}",
DateTime.Now.ToShortTimeString(), message)
);
}
/// Event handler for when the Push Channel Uri changes.
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
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());
uriTxt.Text = pushChannel.ChannelUri.ToString();
});
}
// Event handler for when a Push Notification error occurs.
void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
// Error handling logic for your particular application would be here.
Dispatcher.BeginInvoke(() =>
MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}", e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData)));
}
void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
{
System.Diagnostics.Debug.WriteLine("notifica aperta");
}
}
和樹的PHP例子之一: