推送通知,Uri通道和Php服务器端

时间:2013-04-20 23:06:17

标签: windows windows-phone-7 push-notification mpns

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

  • 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例子:

Pastebin of PHP

1 个答案:

答案 0 :(得分:1)

URI通道对于安装应用程序的每个设备都是唯一的。它必须对每个设备都是唯一的,因为这是告诉MPN服务向哪个设备发送通知的内容。它与Apple推送通知的设备令牌和Google Cloud Messaging的注册ID具有相同的用途。

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

以下是从MSDN获取服务器的一些指导原则:

  

每次您的应用启动时,都应该从推送中传递URI   通道发送推送通知的云服务。它   还建议您将设备ID传递给云服务   这样云服务就可以跟踪URI所在的设备   分配。如果URI发生更改,则云服务可以替换旧服务   该设备ID的URI。 Windows Phone不提供框架   这样做,因为在大多数情况下,应用程序和云服务已经拥有   他们自己用来相互沟通的协议。

     

与云服务通信的最佳做法包括:

     

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

     

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

     

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

     

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

     

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

     

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