c# - Windows Phone silverlight 8.1 Http NotificationChannel ChannelUri 更新未被击中

标签 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 并且 Toast Capable = yes in my Package.appmanifest....我错过了什么运动鞋?

最佳答案

我试过下面的通知代码:

HttpNotificationChannel pushChannel;

将 pushChannel 作为 globle 放入它在 EventHandler 中使用的 App.xaml.cs 文件中

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)
    { }
}

关于c# - Windows Phone silverlight 8.1 Http NotificationChannel ChannelUri 更新未被击中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847796/

相关文章:

c# - StructLayoutAttribute 对 C# 中的属性有什么影响?

c# - 使用 LongListSelector 控件进行导航

silverlight - Silverlight 应用程序中的文化

android - Xamarin.android 默认 FirebaseApp 未在此过程中初始化

javascript - 实现客户端 WebHook 处理程序?

c# - azure-sdk-for-net 在 Mac 上引发异常

c# - 在屏幕上搜索 Pixel

android - 如何防止 Firebase Cloud Messaging (FCM) 中的重复通知

c# - ContexMenuStrip 奇怪的特定怪异行为

c# - System.Core.ni.dll 中发生类型 'System.ArgumentOutOfRangeException' 的异常,但未在用户代码中处理