1

任何Windows Phone开发人员可以帮助我吗?我正在开发一个 Windows Phone 应用程序。当我尝试获取推送通道 uri 时,它适用于模拟器以及设备,但突然我今天遇到了问题。我使用的是相同的代码,但它对于模拟器和pushchannel_uri更新的事件触发工作正常,但是当我尝试从 Windows 手机获取推送通道 uri 时,它没有得到 uri,并且pushchannel_uri更新的事件根本没有触发。

我正在使用Nokia Lumia 510并且我已在应用程序中心注册为开发人员。

我的代码是

public class Cloud2Device
{
    ICloud2DeviceListener eventListener;
    Popup popup=new Popup();

    public Cloud2Device(ICloud2DeviceListener listener)
    {

        Loading loading=new Loading();
        popup.Child = loading;
        popup.IsOpen = true;


        eventListener = listener;
        /// Holds the push channel that is created or found.
        HttpNotificationChannel pushChannel = HttpNotificationChannel.Find(Constants.NOTIFICATION_CHANNEL_NAME);

        // If the channel was not found, then create a new connection to the push service.
        if (pushChannel == null)
        {
            pushChannel = new HttpNotificationChannel(Constants.NOTIFICATION_CHANNEL_NAME);
            // 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);

            // Register for this notification only if you need to receive the notifications while your application is running.
            pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);

            pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived); // Ra

            pushChannel.Open();

            // Bind this new channel for toast events.
            pushChannel.BindToShellToast();
        }
        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);

            // Register for this notification only if you need to receive the notifications while your application is running.
            pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived); // Ra

            pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);


            // Thread.Sleep(5000);
            // 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()));
            string Id = pushChannel.ChannelUri.ToString();
            saveCloudDeviceID(Id);
        }
    }

    public 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()));
               });

        string Id = e.ChannelUri.ToString();
        saveCloudDeviceID(Id);
    }

    private 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))
                 );
    }
    public static string message = "";
    private void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
    {

        using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
        {
            message = reader.ReadToEnd();
        }


        string jsonString = message;

        using (MemoryStream jsonStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ResponseData<LocationResponse>));
            ResponseData<LocationResponse> array = serializer.ReadObject(jsonStream) as ResponseData<LocationResponse>;
        }

        //Deployment.Current.Dispatcher.BeginInvoke(() =>
        //    MessageBox.Show(String.Format("Received Notification {0}:\n{1}",
        //        DateTime.Now.ToShortTimeString(), "notification recieved"))
        //);

    }



    private void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
    {
        StringBuilder message = new StringBuilder();
        string relativeUri = string.Empty;
        message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());

        // Parse out the information that was part of the message.
        foreach (string key in e.Collection.Keys)
        {
            message.AppendFormat("{0}: {1}\n", key, e.Collection [ key ]);

            if (string.Compare(
                key,
                "wp:Param",
                System.Globalization.CultureInfo.InvariantCulture,
                System.Globalization.CompareOptions.IgnoreCase) == 0)
            {
                relativeUri = e.Collection [ key ];
            }
        }

        // Display a dialog of all the fields in the toast.
        Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(message.ToString()));

        //ToastPrompt toast = new ToastPrompt();            
    }

    private void saveCloudDeviceID(string deviceID)
    {
        popup.IsOpen=false;
        WDIsolatedStorageSettings.add(Constants.CLOUD_DEVICE_ID, deviceID);
        WDIsolatedStorageSettings.add(Constants.IS_DEVICE_REGISTERED, true);
        eventListener.DeviceRegistrationCompleted(deviceID);
    }
}

}

谢谢!

4

0 回答 0