【问题标题】:Window Phone silverlight 8.1 HttpNotificationChannel ChannelUriUpdated not being hitWindow Phone silverlight 8.1 HttpNotificationChannel ChannelUriUpdated 未命中
【发布时间】:2015-08-06 05:54:19
【问题描述】:

我有一个非常基本的 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 在我的 Package.appmanifest 中......我错过了什么运动鞋?

【问题讨论】:

  • 哪里有问题?
  • pushChannel.Uri 在 pushChannel.Open() 之后总是为空;并且连接状态已断开。然后当我调用 NotificationHub hub = new NotificationHub(hubName, connectionString);等待 hub.RegisterNativeAsync(pushChannel.ChannelUri.ToString());它在 RegisterNativeAsync 上引发 NullReferenceException
  • 您尝试过我发布的答案吗?

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


【解决方案1】:

我尝试过以下通知代码:

HttpNotificationChannel pushChannel;

将 pushChannel 作为 globle 放在 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.xaml.cs 文件中 App()

活动:

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

【讨论】:

  • 谢谢 - 我应该更精确...在 pushChannel.Open(); pushChannel Uri 为 null 且 connectionstatus 已断开连接。
  • @BzBurr 我在PushChannel_ChannelUriUpdated 事件中得到了 ChannelUri 得到了 ChannelUri
  • @BzBurr 请检查您的配置
  • @BzBurr PushChannel_ChannelUriUpdated 不是每次都调用,而是在HttpNotificationChannel 找不到时调用 OR null
  • ID_CAP_PUSH_NOTIFICATION 已开启,ID_CAP_NETWORKING 已开启,应用支持 Toast...我可能缺少什么?
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
相关资源
最近更新 更多