我想确认我的推送通知实现是否正确。
每次打开我的应用程序时(实际上,我仅在特定页面上注册推送通道,所以每次来回切换页面时),都会创建一个新的推送通道URI,我将其存储在移动服务数据库中以便发送推送通知。这对我来说似乎不正确,因为每次打开应用程序/页面时都会生成一个新的推送通道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服务中。然后,Web服务向通道URI发送通知。当我退出应用程序或页面并返回时,发现推送通道,但会创建一个新的通道URI,我再次保存到我的Web服务中。实际上,我的通道表不断增长。
这种方式是否应该不断生成新的通道URI?对我来说有点不合理。我不确定烤面包和平铺式通知是如何工作的,但我认为当应用程序关闭时,通道URI需要是静态的,以便在应用程序关闭时继续接收通知,但也许bindtotoast和bindtotile的功能可以实现这一点,因此我所做的是正确的,因为它涉及原始通知。