2

我正在尝试使用 WP7.1 中的推送通知。我创建了类和一些 php 类。该示例适用于所有通知类型,但我的问题是 UriChannel。通过我的简单示例,我必须在 php 代码中手动编写 URI 通道。

  • 安装我的应用程序的所有设备的 uri 通道是否唯一?

  • 如何将 Uri Channel 发送到我的服务器?

谢谢。

这是我的代码:

/// 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 示例:

PHP的Pastebin

4

1 回答 1

1

对于安装了您的应用程序的每个设备,URI 通道都是唯一的。它对于每个设备必须是唯一的,因为这就是告诉 MPN 服务将通知发送到哪个设备的原因。它与 Apple 推送通知的设备令牌和谷歌云消息传递的注册 ID 的用途相同。

您可以通过向您的服务器发送一些 HTTP GET 或 POST 请求将 URI 通道发送到您的服务器,并将 URI Chnnael 作为输入参数。

以下是从MSDN获取的一些与服务器通信的指南:

每次您的应用程序启动时,您应该将 URI 从您的推送通道传递到发送推送通知的云服务。还建议您将设备 ID 传递给云服务,以便云服务可以跟踪 URI 分配给哪些设备。如果 URI 发生更改,则云服务可以替换该设备 ID 的旧 URI。Windows Phone 不提供执行此操作的框架,因为在大多数情况下,应用程序和云服务已经有自己的协议,它们用于相互通信。

与云服务通信的最佳实践包括:

该应用程序应通过其相应的云服务进行身份验证。

应用程序应在将 URI 发送到其相应的云服务之前对其通知通道 URI 进行加密。

如果您的云服务将使用 Windows Phone OS 7.0 中不存在的通知属性,那么您应该将操作系统版本信息传递给您的云服务,以便云服务可以正确降级 Windows Phone OS 7.0 客户端的通知。

云服务应验证从其相应应用程序接收到的通知通道 URI 并以安全方式存储它。

从应用程序启动会话时,应始终将通知通道 URI 发送到其相应的云服务。

云服务应该有一个状态码,它可以发送到相应的应用程序,这将触发应用程序创建一个新的通知通道 URI。

于 2013-04-21T03:59:37.710 回答