Merge packaging information to csproj
[platform/core/csapi/push.git] / Tizen.Messaging.Push / Tizen.Messaging.Push / PushClient.cs
1  /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Threading.Tasks;
19
20 namespace Tizen.Messaging.Push
21 {
22     /// <summary>
23     /// The PushClient API provides functions to connect to push service for receiving push messages.
24     /// </summary>
25     /// <remarks>
26     /// The PushClient API provides the way to connect with the push service.
27     /// It provides api's to connect/disconnect from the push service.
28     /// Api's are provided so that an application can register itself
29     /// with the push server along with api's to request push message.
30     /// </remarks>
31     /// <example>
32     /// <code>
33     /// public class Program
34     /// {
35     ///     static void Main(string[] args)
36     ///     {
37     ///         PushClient.PushServiceConnect("xxxxx");
38     ///         Task<ServerResponse> tr = PushClient.PushServerRegister();
39     ///         tr.GetAwaiter().OnCompleted(() => {
40     ///             ServerResponse res = tr.Result;
41     ///             PushClient.GetUnreadNotifications();
42     ///             Task<ServerResponse> tu = PushClient.PushServerUnregister();
43     ///             tu.GetAwaiter().OnCompleted(() => {
44     ///                 PushClient.PushServiceDisconnect();
45     ///             });
46     ///         });
47     ///         PushClient.NotificationReceived += EventHandlerNotificationReceived;
48     ///         PushClient.StateChanged += EventHandlerStateChanged;
49     ///     }
50     /// }
51     /// static void EventHandlerNotificationReceived(object sender, PushMessageEventArgs e)
52     /// {
53     ///     // any user code
54     /// }
55     /// static void EventHandlerStateChanged(object sender, PushConnectionStateEventArgs e)
56     /// {
57     ///     // any user code
58     /// }
59     /// </code>
60     /// </example>
61     public static class PushClient
62     {
63         /// <summary>
64         /// Event Handler for receiving the notifications.
65         /// </summary>
66         public static event EventHandler<PushMessageEventArgs> NotificationReceived
67         {
68             add
69             {
70                 lock (_lock)
71                 {
72                     _notificationReceived += value;
73                 }
74             }
75             remove
76             {
77                 lock (_lock)
78                 {
79                     _notificationReceived -= value;
80                 }
81             }
82         }
83
84         /// <summary>
85         /// Event Handler for receiving changes in States of the connection.
86         /// </summary>
87         public static event EventHandler<PushConnectionStateEventArgs> StateChanged
88         {
89             add
90             {
91                 lock (_lock)
92                 {
93                     _stateChanged += value;
94                 }
95             }
96             remove
97             {
98                 lock (_lock)
99                 {
100                     _stateChanged -= value;
101                 }
102             }
103         }
104
105         /// <summary>
106         /// API to connect with the push service.
107         /// </summary>
108         /// <param name="pushAppId"> The Push Application Id Registered with the server.</param>
109         public static void PushServiceConnect(string pushAppId)
110         {
111             PushImpl.Instance.PushServiceConnect(pushAppId);
112         }
113
114         /// <summary>
115         /// API to disconnect from the push service.
116         /// </summary>
117         public static void PushServiceDisconnect()
118         {
119             PushImpl.Instance.PushServiceDisconnect();
120             //PushImpl.Reset();
121         }
122
123
124         /// <summary>
125         /// API to Register the application with the push server.
126         /// </summary>
127         /// <returns>
128         /// The method returns a task which on completion with give a ServerResponse Object.
129         /// </returns>
130         public static Task<ServerResponse> PushServerRegister()
131         {
132             return PushImpl.Instance.PushServerRegister();
133         }
134
135         /// <summary>
136         /// API to Deregister the application from the push server.
137         /// </summary>
138         /// <returns>
139         /// The method returns a task which on completion with give a ServerResponse Object.
140         /// </returns>
141         public static Task<ServerResponse> PushServerUnregister()
142         {
143             return PushImpl.Instance.PushServerUnregister();
144         }
145
146         /// <summary>
147         /// Gets the unread notifications for the application.
148         /// </summary>
149         public static void GetUnreadNotifications()
150         {
151             PushImpl.Instance.GetUnreadNotifications();
152         }
153
154         /// <summary>
155         /// registration Id received from server. </summary>
156         /// <returns>
157         /// It is the string which is the Id received from the server.
158         /// </returns>
159         public static string GetRegistrationId()
160         {
161             return PushImpl.Instance.GetRegistrationId();
162         }
163
164         internal static void StateChange(PushConnectionStateEventArgs args)
165         {
166             if (_stateChanged != null)
167                 _stateChanged(null, args);
168         }
169
170         internal static void Notify(PushMessageEventArgs args)
171         {
172             if (_notificationReceived != null)
173                 _notificationReceived(null, args);
174         }
175
176         private static object _lock = new object();
177         private static event EventHandler<PushMessageEventArgs> _notificationReceived;
178         private static event EventHandler<PushConnectionStateEventArgs> _stateChanged;
179     }
180 }