加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Windows Phone 8推送通知推送通道始终创建新的通道uri

发布时间:2020-12-16 01:57:33 所属栏目:百科 来源:网络整理
导读:我想检查我的推送通知实现是否正确. 每次我打开我的应用程序(实际上我只在特定页面上注册推送通道,所以每次我从该页面来回移动)都会创建一个新的推送通道URI,我将其存储在我的移动服务数据库中以发送推送通知.这对我来说似乎不正确,因为每次打开app / page时
我想检查我的推送通知实现是否正确.

每次我打开我的应用程序(实际上我只在特定页面上注册推送通道,所以每次我从该页面来回移动)都会创建一个新的推送通道URI,我将其存储在我的移动服务数据库中以发送推送通知.这对我来说似乎不正确,因为每次打开app / page时都会生成一个新的推送通道URI,因此每个使用我的应用程序的设备的通道URI列表都会增长和增长.我假设您创建了一个推送通道,存储通道URI并根据需要推送到它.我会在这里注意到我正在使用原始推送通知.

我知道推送频道每隔一段时间就会过期,但对我而言,每次我退出应用程序/页面时都会发生这种情况,因此当调用onNavigateTo时,我发现确实存在的推送频道,并且始终创建新的频道URI.它是否正确?

我的代码如下:

protected override void OnNavigatedTo(NavigationEventArgs e)
????????{
???????????registerPushChannel();
?????????}

private void registerPushChannel()
    {
        // The name of our push channel.
        string channelName = "RawSampleChannel";

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

            // code which passes the new channel URI back to my web service               

        }

    }

protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        pushChannel.Close();
    }

因此,为了澄清,应用程序已打开,推送频道已注册,频道uri已保存在我的网络服务中.然后,Web服务将通知发送到通道uri.当我退出应用程序或页面并返回它时,找到推送通道但创建了一个新的通道uri,我再次将其保存到我的Web服务.我的频道表实际上正在不断增长和发展.

那么它应该如何与不断生成的新通道URI一起使用?这对我来说没什么意义.我不确定烤面包和磁贴通知是如何工作的,但我认为当应用程序关闭时,通道URI需要是静态的,以便在应用程序关闭时继续接收通知,但也许这可能是bindtotoast和bindtotile的功能,所以我正在做的是正确的,因为它与原始通知有关.

解决方法

你大部分都做对了.

推送通知是一件有趣的事情.
您创建一个频道,将其发送到您的服务器,然后服务器可以发送直到它失败(频道Uri过期或出现错误).
此时,应用程序需要创建一个新的ChannelUri,然后在服务器上更新为该应用程序/设备存储的值.然后,服务器将能够发送通知.

一些重点

>当一个新的频道Uri被请求一个仍然有效的频道时,你会得到同一个频道.
>当您要求新频道uri和当前频道已经过期时,您通常会返回相同的uri,但该频道将再次播出.
>如果没有像registerPushChannel方法那样运行代码,就无法知道某个频道是否已在应用中过期. (除非您在后端跟踪此信息,并且应用程序会查询后端.)
>无法告诉应用程序频道已过期,或者告诉用户重新打开应用程序以使用推送基础架构重新建立频道连接.

尝试确保频道始终可用的标准方法是在应用启动时检查频道.这就是你正在做的事情,你可能只是想确保你更新服务器记录而不仅仅是添加更多.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读