WP7 Push Notifications returns null channel uri

Stavros Koureas

I have an app in windows phone marketplace and i have setted the push notifications with the code from http://msdn.microsoft.com/

public MainPage()
    {
        /// Holds the push channel that is created or found.
        HttpNotificationChannel pushChannel;

        // The name of our push channel.
        string channelName = "ToastSampleChannel";

        InitializeComponent();

        // Try to find the push channel.
        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);

            // 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.
            System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
            MessageBox.Show(String.Format("Channel Uri is {0}",
                pushChannel.ChannelUri.ToString()));

        }
    }

For some time the code was returning the uri, but after 2-3 months was returning null as pushChannel.ChannelUriUpdated never triggered! Can i do something to solve this problem?

Kristian Vukusic

You also need to check whether the pushChannel.ChannelUri is null and if it is you create a new channel.

Here is the code when pushChannel != null, in your case this goes into the else clause:

if (pushChannel.ChannelUri != null)
{
    // This is raising my event to signal any subscribers
    // that an new channelUri is available
    RaiseGotPushUri(pushChannel.ChannelUri);

    // Re-register the event handlers
    pushChannel.ChannelUriUpdated += PushChannel_ChannelUriUpdated;
    pushChannel.ShellToastNotificationReceived += PushChannel_ShellToastNotificationReceived;
    pushChannel.ErrorOccurred += PushChannel_ErrorOccurred;
}
else
{
    // If we never got the Uri back, unbind and reset everything...
    // Dispose of the old channel
    pushChannel.ChannelUriUpdated -= PushChannel_ChannelUriUpdated;
    pushChannel.ShellToastNotificationReceived -= PushChannel_ShellToastNotificationReceived;
    pushChannel.ErrorOccurred -= PushChannel_ErrorOccurred;

    if (pushChannel.IsShellToastBound) pushChannel.UnbindToShellToast();
    pushChannel.Close();
    pushChannel.Dispose();

    // ... and re-register the event handlers
    pushChannel = new HttpNotificationChannel(channelName);//, _serviceName);
    pushChannel.ChannelUriUpdated += PushChannel_ChannelUriUpdated;
    pushChannel.ShellToastNotificationReceived += PushChannel_ShellToastNotificationReceived;
    pushChannel.ErrorOccurred += PushChannel_ErrorOccurred;

    // Ask for a new Uri
    pushChannel.Open();
    // Set the HttpNotificationChannel to handle the appropriate push notifications
    pushChannel.BindToShellToast();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

WP7推送通知返回空通道uri

来自分类Dev

Android Parse Push Notifications很慢?

来自分类Dev

iOS:Apple Push Notifications理解

来自分类Dev

Uri getHost()返回null

来自分类Dev

Uri getHost()返回null

来自分类Dev

如何测试Apple Push Notifications反馈服务?

来自分类Dev

CloudKit for sending push notifications through cron jobs?

来自分类Dev

Java Servlet does not stop Push notifications

来自分类Dev

WebApi应用程序的Apple Push Notifications

来自分类Dev

Windows Server上的Apple Push Notifications证书

来自分类Dev

如何测试Apple Push Notifications反馈服务?

来自分类Dev

WP7:访问内置事件

来自分类Dev

倍数消息框-WP7

来自分类Dev

在wp7中禁用ApplicationBarIconButton

来自分类Dev

目标WP7和WP8.1(8.0设备将获得WP7应用程序)吗?

来自分类Dev

Method override returns null

来自分类Dev

实时聊天等应用的Signal R Vs Push Notifications

来自分类Dev

Apple Push Notifications:没有收到设备令牌?

来自分类Dev

Windows Server 2008上的Apple Push Notifications无法正常工作

来自分类Dev

Django多模型继承,django-push-notifications

来自分类Dev

如何在Bluemix Push Notifications服务中注册userId?

来自分类Dev

从Web角度看Notifications API和Push API之间的区别

来自分类Dev

适用于服务器的Apple Push Notifications服务

来自分类Dev

Apple Push Notifications在虚拟机上不起作用

来自分类Dev

Image upload from WP7 to Web Api

来自分类Dev

IsolatedStorageFile.GetLastAccessTime在wp7上崩溃

来自分类Dev

在wp7上运行项目时TargetInvocationException

来自分类Dev

WP7 ListBox所选项目未更改颜色

来自分类Dev

将图像从WP7上传到Web Api