[NUI] Sync dalihub/TizenFX and Samsung/TizenFX (#528)
[platform/core/csapi/tizenfx.git] / src / Tizen.Messaging.Push / Tizen.Messaging.Push / PushClient.cs
1  /*
2  * Copyright (c) 2018 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     public static class PushClient
33     {
34         /// <summary>
35         /// Event Handler for receiving the notifications.
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         public static event EventHandler<PushMessageEventArgs> NotificationReceived
39         {
40             add
41             {
42                 lock (_lock)
43                 {
44                     _notificationReceived += value;
45                 }
46             }
47             remove
48             {
49                 lock (_lock)
50                 {
51                     _notificationReceived -= value;
52                 }
53             }
54         }
55
56         /// <summary>
57         /// Event Handler for receiving changes in States of the connection.
58         /// </summary>
59         /// <since_tizen> 3 </since_tizen>
60         public static event EventHandler<PushConnectionStateEventArgs> StateChanged
61         {
62             add
63             {
64                 lock (_lock)
65                 {
66                     _stateChanged += value;
67                 }
68             }
69             remove
70             {
71                 lock (_lock)
72                 {
73                     _stateChanged -= value;
74                 }
75             }
76         }
77
78         /// <summary>
79         /// API to connect with the push service.
80         /// </summary>
81         /// <since_tizen> 3 </since_tizen>
82         /// <privilege>http://tizen.org/privilege/push</privilege>
83         /// <exception cref="InvalidOperationException"> In case of privilege not defined. </exception>
84         /// <param name="pushAppId"> The Push Application ID Registered with the server.</param>
85         public static void PushServiceConnect(string pushAppId)
86         {
87             PushImpl.PushServiceConnect(pushAppId);
88         }
89
90         /// <summary>
91         /// API to disconnect from the push service.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         public static void PushServiceDisconnect()
95         {
96             PushImpl.PushServiceDisconnect();
97             //PushImpl.Reset();
98         }
99
100
101         /// <summary>
102         /// API to Register the application with the push server.
103         /// </summary>
104         /// <since_tizen> 3 </since_tizen>
105         /// <returns>
106         /// The method returns a task, which on completion will give a ServerResponse Object.
107         /// </returns>
108         public static Task<ServerResponse> PushServerRegister()
109         {
110             return PushImpl.PushServerRegister();
111         }
112
113         /// <summary>
114         /// API to Deregister the application from the push server.
115         /// </summary>
116         /// <since_tizen> 3 </since_tizen>
117         /// <returns>
118         /// The method returns a task, which on completion will give a ServerResponse Object.
119         /// </returns>
120         public static Task<ServerResponse> PushServerUnregister()
121         {
122             return PushImpl.PushServerUnregister();
123         }
124
125         /// <summary>
126         /// Gets the unread notifications for the application.
127         /// </summary>
128         /// <since_tizen> 3 </since_tizen>
129         public static void GetUnreadNotifications()
130         {
131             PushImpl.GetUnreadNotifications();
132         }
133
134         /// <summary>
135         /// registration ID received from server.
136         /// </summary>
137         /// <since_tizen> 3 </since_tizen>
138         /// <returns>
139         /// It is the string, which is the ID received from the server.
140         /// </returns>
141         public static string GetRegistrationId()
142         {
143             return PushImpl.GetRegistrationId();
144         }
145
146         internal static void StateChange(PushConnectionStateEventArgs args)
147         {
148             if (_stateChanged != null)
149                 _stateChanged(null, args);
150         }
151
152         internal static void Notify(PushMessageEventArgs args)
153         {
154             if (_notificationReceived != null)
155                 _notificationReceived(null, args);
156         }
157
158         private static object _lock = new object();
159         private static event EventHandler<PushMessageEventArgs> _notificationReceived;
160         private static event EventHandler<PushConnectionStateEventArgs> _stateChanged;
161     }
162 }