예제 #1
0
        /// <summary>
        ///     Registers a device in Azure Notification Hub.
        /// </summary>
        /// <remarks>
        ///     This first unregisters all existing user registrations
        ///     for the specified <paramref name="userId"/>.
        /// </remarks>
        /// <param name="userId">The user to subscribe.</param>
        /// <param name="platform">The push notification platform.</param>
        /// <param name="handle">Device handle.</param>
        public virtual async Task RegisterAsync(Guid userId, PushNotificationPlatform platform, string handle)
        {
            // First clear the existing registration if it exists
            if (await _notificationRegistrationRepository.ExistsAsync(userId))
            {
                var currentRegistration = await _notificationRegistrationRepository.GetAsync(userId);

                await _notificationClient.UnregisterAsync(currentRegistration);

                await _notificationRegistrationRepository.DeleteAsync(currentRegistration.Id);
            }

            // Create new registration and assign external id
            var registration = await _notificationClient.RegisterAsync(new NotificationRegistration
            {
                Handle = handle,
                PushNotificationPlatform = platform,
                Id = userId
            });

            await _notificationRegistrationRepository.CreateAsync(registration);
        }