Added API level
[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     /// <since_tizen> 3 </since_tizen>
26     /// <remarks>
27     /// The PushClient API provides the way to connect with the push service.
28     /// It provides api's to connect/disconnect from the push service.
29     /// Api's are provided so that an application can register itself
30     /// with the push server along with api's 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         /// <param name="pushAppId"> The Push Application Id Registered with the server.</param>
113         public static void PushServiceConnect(string pushAppId)
114         {
115             PushImpl.Instance.PushServiceConnect(pushAppId);
116         }
117
118         /// <summary>
119         /// API to disconnect from the push service.
120         /// </summary>
121         /// <since_tizen> 3 </since_tizen>
122         public static void PushServiceDisconnect()
123         {
124             PushImpl.Instance.PushServiceDisconnect();
125             //PushImpl.Reset();
126         }
127
128
129         /// <summary>
130         /// API to Register the application with the push server.
131         /// </summary>
132         /// <since_tizen> 3 </since_tizen>
133         /// <returns>
134         /// The method returns a task which on completion with give a ServerResponse Object.
135         /// </returns>
136         public static Task<ServerResponse> PushServerRegister()
137         {
138             return PushImpl.Instance.PushServerRegister();
139         }
140
141         /// <summary>
142         /// API to Deregister the application from the push server.
143         /// </summary>
144         /// <since_tizen> 3 </since_tizen>
145         /// <returns>
146         /// The method returns a task which on completion with give a ServerResponse Object.
147         /// </returns>
148         public static Task<ServerResponse> PushServerUnregister()
149         {
150             return PushImpl.Instance.PushServerUnregister();
151         }
152
153         /// <summary>
154         /// Gets the unread notifications for the application.
155         /// </summary>
156         /// <since_tizen> 3 </since_tizen>
157         public static void GetUnreadNotifications()
158         {
159             PushImpl.Instance.GetUnreadNotifications();
160         }
161
162         /// <summary>
163         /// registration Id received from server. </summary>
164         /// <since_tizen> 3 </since_tizen>
165         /// <returns>
166         /// It is the string which is the Id received from the server.
167         /// </returns>
168         public static string GetRegistrationId()
169         {
170             return PushImpl.Instance.GetRegistrationId();
171         }
172
173         internal static void StateChange(PushConnectionStateEventArgs args)
174         {
175             if (_stateChanged != null)
176                 _stateChanged(null, args);
177         }
178
179         internal static void Notify(PushMessageEventArgs args)
180         {
181             if (_notificationReceived != null)
182                 _notificationReceived(null, args);
183         }
184
185         private static object _lock = new object();
186         private static event EventHandler<PushMessageEventArgs> _notificationReceived;
187         private static event EventHandler<PushConnectionStateEventArgs> _stateChanged;
188     }
189 }