Question

I'm trying to use the push notifications in WP7.1. I created the class and some php classes. The example works for all the notification types, but my problem is the UriChannel. With my simple example I have to write manually the URI channel in the php code.

  • Is the uri channel unique for all of the devices where my app is installed?

  • How can I send the Uri Channel to my server?

Thanks.

This is my code:

/// 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");
    }


}

And one of the tree php example:

Pastebin of PHP

Était-ce utile?

La solution

The URI Channel is unique for each device on which your application is installed. It has to be unique for each device, since that's what tells the MPN service which device to send the notification to. It serves the same purpose as Device Tokens for Apple Push Notifications and Registration IDs for Google Cloud Messaging.

You can send the URI Channel to your server by sending some HTTP GET or POST request to your server, with the URI Chnnael as the input parameter.

Here are some guidelines for comminicating your server taken from MSDN :

Each time your app starts, you should pass the URI from your push channel to the cloud service that sends out the push notifications. It is also recommended that you pass the device ID to your cloud service so that the cloud service can track to which devices the URIs are assigned. If a URI changes, then the cloud service can replace the old URI for that device ID. Windows Phone does not provide a framework to do this, since in most scenarios apps and cloud services already have their own protocols that they use to communicate with each other.

Best practices for communicating with your cloud service include:

The app should authenticate with its corresponding cloud service.

The app should encrypt its notification channel URI before sending the URI to its corresponding cloud service.

If your cloud service will be using notifications properties that do not exist in Windows Phone OS 7.0 then you should pass the OS version information to your cloud service so that the cloud service can correctly downgrade the notifications for Windows Phone OS 7.0 clients.

The cloud service should validate the notification channel URI received from its corresponding app and store it in a secure manner.

When a session is initiated from the app, the notification channel URI should always be sent to its corresponding cloud service.

The cloud service should have a status code that it can send to its corresponding app that will trigger the app to create a new notification channel URI.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top