Window Phone silverlight 8.1 HttpNotificationChannel ChannelUriUpdated未被点击

时间:2015-08-06 05:54:19

标签: c# silverlight push-notification windows-phone-8.1

我有一个非常基本的Windows Phone Silverlight 8.1应用程序,其中包含以下内容(我想在将其添加到更大的预先存在的应用程序之前证明该概念):

HttpNotificationChannel pushChannel;

void registerPushChannel()
{
   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.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);

      pushChannel.Open();

   }
   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.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);

      // code which passes the new channel URI back to my web service     
    }
}

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());
       MessageBox.Show(String.Format("Channel Uri is {0}",
                    e.ChannelUri.ToString()));

     });
 }

问题是PushChannel_ChannelUriUpdated从未被击中,我只是想弄清楚原因!

我已经在我的WMAppManifest.xml中设置了ID_CAP_PUSH_NOTIFICATION 在我的Package.appmanifest中Toast Capable = yes ....我错过了哪些诈骗?

1 个答案:

答案 0 :(得分:0)

我曾尝试过下面的通知代码:

HttpNotificationChannel pushChannel;

将pushChannel置于App.xaml.cs文件中,并在EventHandler

中使用
string channelName = "PushChannel";
pushChannel = HttpNotificationChannel.Find(channelName);
//Push Notifications
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);

    // 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.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.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);

    // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.enter code here
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        //pushURI = pushChannel.ChannelUri.ToString();
        //MessageBox.Show(String.Format("Channel Uri is {0}", pushChannel.ChannelUri.ToString()));
        Notification.ChannelURI = pushChannel.ChannelUri.ToString();
    });
}

将以上代码放在App()

中的App.xaml.cs文件中

活动:

void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e1)
{
    try
    {
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            MessageBox.Show(String.Format("Channel Uri is {0}", e1.ChannelUri.ToString()));
        });
    }
    catch (Exception ex)
    { }
}
void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e2)
{
    try
    {
        // 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}", e2.ErrorType, e2.Message, e2.ErrorCode, e2.ErrorAdditionalData));
        });
    }
    catch (Exception ex)
    { }
}