티스토리 뷰

반응형

이번 포스트는 나 빼고 다른 모든 사람들에게 노티를 보내기!를 구현하기 위한 가장 마지막 단계인것 같다.

이 내용에 대한 작업을 하기 위해 Notification Hubs에 대한 문서를 간단하게 읽어 보아야 한다.

 

 

1. 참고

알림 허브 기능(한글)

http://msdn.microsoft.com/ko-kr/library/dn530752.aspx

 

Notification Hubs Features

http://msdn.microsoft.com/en-us/library/dn530752.aspx

 

위의 링크로 이동해서 일단 어떤 개념인지에 대한 파악을 한다.

 

 

2. 특히 중요한 부분은

 

Routing and Tag Expressions

http://msdn.microsoft.com/en-us/library/dn530749.aspx

 

이 부분이다.

 

 

앱이 실행이 되면 PNS 서버로 부터 자신을 가르키는 주소를 받아 온다. 받아온 주소를 Notification Hubs에 등록을 하게 되는데, 이때, 사용자 인증이 되어 있다면, 그 정보를 Tag로 사용 한다. 이 내용은 조금전에 소스를 통해서 알게 되었는데. 다음과 같이 확인 하면 된다.

 

 

 

 

 

RegisterNativeAsync를 실행한 후에 자신이 등록되어 있는 서비스를 조회하면 1개의 WnsRegistration이 나오는데, 그 안에 Tags에 자신의 인증에 사용된 ID가 나오는 것을 알 수 있다.

 

**********

서버단에 PushRegistrationHandler 코드에 사용자 ID를 Tag로 등록하는 곳이 있다.

ValidateTags 메소드에 custom:을 추가해서 사용자 정의 인증된 사용자도 확인이 가능하도록 한다.

 

    public class PushRegistrationHandler : INotificationHandler
    {
        public Task Register(ApiServices services, HttpRequestContext context, NotificationRegistration registration)
        {
            try
            {
                // Perform a check here for user ID tags, which are not allowed.
                if (!ValidateTags(registration))
                {
                    throw new InvalidOperationException(
                        "You cannot supply a tag that is a user ID.");
                }

                // Get the logged-in user.
                var currentUser = context.Principal as ServiceUser;

 

                // Add a new tag that is the user ID.
                if (currentUser != null)
                {
                    registration.Tags.Add(currentUser.Id);
                    services.Log.Info("Registered tag for userId: " + currentUser.Id);
                }
            }
            catch (Exception ex)
            {
                services.Log.Error(ex.ToString());
            }
            return Task.FromResult(true);
        }

        public Task Unregister(ApiServices services, HttpRequestContext context, string deviceId)
        {
            // This is where you can hook into registration deletion.
            return Task.FromResult(true);
        }

 

        private bool ValidateTags(NotificationRegistration registration)
        {
            // Create a regex to search for disallowed tags.
            var searchTerm =
            new System.Text.RegularExpressions.Regex(@"facebook:|google:|twitter:|microsoftaccount:|custom:",
                System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            foreach (string tag in registration.Tags)
            {
                if (searchTerm.IsMatch(tag))
                {
                    return false;
                }
            }
            return true;
        }
    }

 

 

그럼, 이 이야기는 사용자 인증이 완료된 클라이언트가 Notification Hubs에 등록이 되면 기본적으로 자신의 id를 Tag로 가지고 있다는 것이니, Push를 날릴때 자기 자신을 지칭하는 Tag만 제외 시키면 되는 것이다.

 

 

즉,

 

* 서버단에서 Push.SendAsync를 하는 곳

 

            try
            {
                var currentUser = User as ServiceUser;
                if (currentUser != null)
                {
                    var result = await Services.Push.SendAsync(message, string.Format("!{0}", currentUser.Id));
                    Services.Log.Info(result.State.ToString());
                }
            }
            catch (System.Exception ex)
            {
                Services.Log.Error(ex.Message, null, "Push.SendAsync Error");
            }

 

서버단의 Azure Mobile Service에서 서비스를 호출한 사람이 currentUser이니, Tag Expression

string.Format("!{0}", currentUser.Id) 이렇게 붙인다면, !custom:f2f0fc0cxxxxxxxxx가 되는 것이다.

즉, 자신의 테그를 제외한 나머지에게 발송을 하라는 뜻이 된다.

 

 

이렇게 해서 나 제외한 다른 모든 사람에게 Push를 보내는 것을 완료했다.

 

반응형
댓글