Windows 8.1的推送通知

3

我心中所想的:

  1. 应用程序获取URI。

  2. 应用程序将URI发送到我的数据库。

  3. 管理员面板,管理员向所有存在于数据库中的URI发送消息,该消息作为通知发送。

我找到了这个代码,它可以向我的数据库中的URI发送通知。

然而,我无法生成URI并将其发送到服务器。我尝试使用以下代码:

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

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

        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);
            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);

            // 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()));

        }
    }

但是Visual Studio无法识别HttpNotificationChannel。我已经尝试添加'using Microsoft.Phone.Notification',但它在Microsoft包中找不到Phone。我认为这对于Windows 8.1已经被弃用了?我是Windows的新手,我可以将其与Android的GCM相关联,并为Android应用程序实施相同的操作。

我如何获取Windows手机的URI并将其发送到服务器?

1个回答

2

像这样做:

var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
var uri = channel.Uri

这里有一个很好的示例(链接)。不要忘记你需要将应用程序与商店关联,包括创建应用程序并注册WNS服务以获取客户端密钥。


谢谢,创建客户端秘钥后可以正常工作了。您能帮我解决如何接收原始通知的问题吗?我用的代码是使用“HttpNotificationChanel”,但它不起作用。 - Pranjal
只需使用我提供的样本。其中有一个名为Scenario3_Listening.xaml.cs的文件,其中包含了监听部分。这是一个完整的客户端应用程序,涵盖了所有方面。 - norekhov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接