1

Windows Phone の開発者が助けてくれますか? 私はWindows Phoneアプリケーションを開発しています。プッシュ チャネル uri を取得しようとすると、エミュレータでもデバイスでも問題なく動作しましたが、今日突然問題が発生しました。同じコードを使用していますが、エミュレーターとpushchannel_uri更新されたイベントのトリガーでは正常に動作していますが、Windows Phone からプッシュ チャネル 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