在Windows Phone 8.1设备中,pushChannel始终为null(推送通知)

时间:2014-10-20 11:08:17

标签: c# windows-phone-8 push-notification windows-phone-8.1 mpns

pushChannel = HttpNotificationChannel.Find(channelName);

但每次运行应用程序时都会返回null

仅在wp8中正在注册频道的Windows Phone 8.1设备中提供null

2 个答案:

答案 0 :(得分:0)

如果它将推送频道返回为" null",那么您是否尝试创建推送频道?

// 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);  <---- Create a channel if it doesn't exist
    ######################################################################
        // 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()));

    }

答案 1 :(得分:0)

 PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

Debug.WriteLine(“Channel ::”+ channel.Uri.ToString());