如何检查设备是否注册了Windows phone推送通知



我正在使用通知中心注册我的windows手机设备的推送通知服务。如果我注册我的设备,如果我再次注册我的设备推送通知,我收到两个通知,这意味着一个设备注册了两次。谁能告诉我如何防止一个用户注册不止一次。

我的代码如下:
    public static async Task SetupPushNotifications()
    {  
      await RegisterWithNotificationHub();
    }
    private static HttpNotificationChannel CreateHttpNotificationChannel(string channelName)
    {
        var httpChannel = HttpNotificationChannel.Find(channelName);
        #endregion   
        return httpChannel;
    }
    private static async Task RegisterWithNotificationHub()
        {
            try
            {
                // requesting a channel from MPNS
                App.NotificationChannel = CreateHttpNotificationChannel("");
                App.ClientHub = new NotificationHub(
                    "",""
                    );
                var storedTagsForUser = await GetRegistrationsTagsFromBackEnd();
                await RegisterTemplateNotificationWithNotificationHub(storedTagsForUser);
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc);
            }
        }
     private static async Task RegisterTemplateNotificationWithNotificationHub(IEnumerable<string> tags)
        {
            var toastMessageTemplate =
                    "<?xml version="1.0" encoding="utf-8"?>" +
                        "<wp:Notification xmlns:wp="WPNotification">" +
                            "<wp:Toast>" +
                                "<wp:Text1>$(oppTitleValue)</wp:Text1>" +
                                "<wp:Text2>$(myToastMessage)</wp:Text2>" +
                                "<wp:Param>$(pageToOpen)</wp:Param>" +
                            "</wp:Toast>" +
                        "</wp:Notification>";
            try
            {
                await App.ClientHub.RegisterTemplateAsync(
                        App.NotificationChannel.ChannelUri.AbsoluteUri,
                        xmlTemplate: toastMessageTemplate,
                        templateName: TemplateRegistrationName,
                        tags: tags);
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Error registering template notification with notification hubs: " + exc);
            }
        }

您可以通过调用HttpNotificationChannel.Find(channelName)来检查通道是否已经存在。如果没有,它将返回null。

所以你只会想要创建一个通道,如果它不存在。例如

private void RegisterPushChannel()
{
    HttpNotificationChannel currentChannel = HttpNotificationChannel.Find("MyPushChannel");
    if (currentChannel == null)
    {
        currentChannel = new HttpNotificationChannel("MyPushChannel");
        currentChannel.Open();
        currentChannel.BindToShellTile();
        currentChannel.BindToShellToast();
    }
}

最新更新