2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Threading.Tasks;
20 namespace Tizen.Messaging.Push
23 /// The PushClient API provides functions to connect to push service for receiving push messages.
25 /// <since_tizen> 3 </since_tizen>
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.
34 /// public class Program
36 /// static void Main(string[] args)
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();
48 /// PushClient.NotificationReceived += EventHandlerNotificationReceived;
49 /// PushClient.StateChanged += EventHandlerStateChanged;
52 /// static void EventHandlerNotificationReceived(object sender, PushMessageEventArgs e)
56 /// static void EventHandlerStateChanged(object sender, PushConnectionStateEventArgs e)
62 public static class PushClient
65 /// Event Handler for receiving the notifications.
67 /// <since_tizen> 3 </since_tizen>
68 public static event EventHandler<PushMessageEventArgs> NotificationReceived
74 _notificationReceived += value;
81 _notificationReceived -= value;
87 /// Event Handler for receiving changes in States of the connection.
89 /// <since_tizen> 3 </since_tizen>
90 public static event EventHandler<PushConnectionStateEventArgs> StateChanged
96 _stateChanged += value;
103 _stateChanged -= value;
109 /// API to connect with the push service.
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)
117 PushImpl.Instance.PushServiceConnect(pushAppId);
121 /// API to disconnect from the push service.
123 /// <since_tizen> 3 </since_tizen>
124 public static void PushServiceDisconnect()
126 PushImpl.Instance.PushServiceDisconnect();
132 /// API to Register the application with the push server.
134 /// <since_tizen> 3 </since_tizen>
136 /// The method returns a task which on completion with give a ServerResponse Object.
138 public static Task<ServerResponse> PushServerRegister()
140 return PushImpl.Instance.PushServerRegister();
144 /// API to Deregister the application from the push server.
146 /// <since_tizen> 3 </since_tizen>
148 /// The method returns a task which on completion with give a ServerResponse Object.
150 public static Task<ServerResponse> PushServerUnregister()
152 return PushImpl.Instance.PushServerUnregister();
156 /// Gets the unread notifications for the application.
158 /// <since_tizen> 3 </since_tizen>
159 public static void GetUnreadNotifications()
161 PushImpl.Instance.GetUnreadNotifications();
165 /// registration Id received from server. </summary>
166 /// <since_tizen> 3 </since_tizen>
168 /// It is the string which is the Id received from the server.
170 public static string GetRegistrationId()
172 return PushImpl.Instance.GetRegistrationId();
175 internal static void StateChange(PushConnectionStateEventArgs args)
177 if (_stateChanged != null)
178 _stateChanged(null, args);
181 internal static void Notify(PushMessageEventArgs args)
183 if (_notificationReceived != null)
184 _notificationReceived(null, args);
187 private static object _lock = new object();
188 private static event EventHandler<PushMessageEventArgs> _notificationReceived;
189 private static event EventHandler<PushConnectionStateEventArgs> _stateChanged;