Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / 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     /// <since_tizen> 3 </since_tizen>
26     /// <remarks>
27     /// The PushClient API provides the way to connect with the push service.
28     /// It provides APIs to connect/disconnect from the push service.
29     /// APIs are provided so that an application can register itself
30     /// with the push server along with APIs to request push message.
31     /// </remarks>
32     /// <example>
33     /// <code>
34     /// public class Program
35     /// {
36     ///     static void Main(string[] args)
37     ///     {
38     ///         PushClient.PushServiceConnect("xxxxx");
39     ///         Task<ServerResponse> tr = PushClient.PushServerRegister();
40     ///         tr.GetAwaiter().OnCompleted(() => {
41     ///             ServerResponse res = tr.Result;
42     ///             PushClient.GetUnreadNotifications();
43     ///             Task<ServerResponse> tu = PushClient.PushServerUnregister();
44     ///             tu.GetAwaiter().OnCompleted(() => {
45     ///                 PushClient.PushServiceDisconnect();
46     ///             });
47     ///         });
48     ///         PushClient.NotificationReceived += EventHandlerNotificationReceived;
49     ///         PushClient.StateChanged += EventHandlerStateChanged;
50     ///     }
51     /// }
52     /// static void EventHandlerNotificationReceived(object sender, PushMessageEventArgs e)
53     /// {
54     ///     // any user code
55     /// }
56     /// static void EventHandlerStateChanged(object sender, PushConnectionStateEventArgs e)
57     /// {
58     ///     // any user code
59     /// }
60     /// </code>
61     /// </example>
62     public static class PushClient
63     {
64         /// <summary>
65         /// Event Handler for receiving the notifications.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         public static event EventHandler<PushMessageEventArgs> NotificationReceived
69         {
70             add
71             {
72                 lock (_lock)
73                 {
74                     _notificationReceived += value;
75                 }
76             }
77             remove
78             {
79                 lock (_lock)
80                 {
81                     _notificationReceived -= value;
82                 }
83             }
84         }
85
86         /// <summary>
87         /// Event Handler for receiving changes in States of the connection.
88         /// </summary>
89         /// <since_tizen> 3 </since_tizen>
90         public static event EventHandler<PushConnectionStateEventArgs> StateChanged
91         {
92             add
93             {
94                 lock (_lock)
95                 {
96                     _stateChanged += value;
97                 }
98             }
99             remove
100             {
101                 lock (_lock)
102                 {
103                     _stateChanged -= value;
104                 }
105             }
106         }
107
108         /// <summary>
109         /// API to connect with the push service.
110         /// </summary>
111         /// <since_tizen> 3 </since_tizen>
112         /// <privilege>http://tizen.org/privilege/push</privilege>
113         /// <exception cref="InvalidOperationException"> In case of privilege not defined. </exception>
114         /// <param name="pushAppId"> The Push Application ID Registered with the server.</param>
115         public static void PushServiceConnect(string pushAppId)
116         {
117             PushImpl.Instance.PushServiceConnect(pushAppId);
118         }
119
120         /// <summary>
121         /// API to disconnect from the push service.
122         /// </summary>
123         /// <since_tizen> 3 </since_tizen>
124         public static void PushServiceDisconnect()
125         {
126             PushImpl.Instance.PushServiceDisconnect();
127             //PushImpl.Reset();
128         }
129
130
131         /// <summary>
132         /// API to Register the application with the push server.
133         /// </summary>
134         /// <since_tizen> 3 </since_tizen>
135         /// <returns>
136         /// The method returns a task, which on completion will give a ServerResponse Object.
137         /// </returns>
138         public static Task<ServerResponse> PushServerRegister()
139         {
140             return PushImpl.Instance.PushServerRegister();
141         }
142
143         /// <summary>
144         /// API to Deregister the application from the push server.
145         /// </summary>
146         /// <since_tizen> 3 </since_tizen>
147         /// <returns>
148         /// The method returns a task, which on completion will give a ServerResponse Object.
149         /// </returns>
150         public static Task<ServerResponse> PushServerUnregister()
151         {
152             return PushImpl.Instance.PushServerUnregister();
153         }
154
155         /// <summary>
156         /// Gets the unread notifications for the application.
157         /// </summary>
158         /// <since_tizen> 3 </since_tizen>
159         public static void GetUnreadNotifications()
160         {
161             PushImpl.Instance.GetUnreadNotifications();
162         }
163
164         /// <summary>
165         /// registration ID received from server. </summary>
166         /// <since_tizen> 3 </since_tizen>
167         /// <returns>
168         /// It is the string, which is the ID received from the server.
169         /// </returns>
170         public static string GetRegistrationId()
171         {
172             return PushImpl.Instance.GetRegistrationId();
173         }
174
175         internal static void StateChange(PushConnectionStateEventArgs args)
176         {
177             if (_stateChanged != null)
178                 _stateChanged(null, args);
179         }
180
181         internal static void Notify(PushMessageEventArgs args)
182         {
183             if (_notificationReceived != null)
184                 _notificationReceived(null, args);
185         }
186
187         private static object _lock = new object();
188         private static event EventHandler<PushMessageEventArgs> _notificationReceived;
189         private static event EventHandler<PushConnectionStateEventArgs> _stateChanged;
190     }
191 }