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 /// <param name="pushAppId"> The Push Application Id Registered with the server.</param>
113 public static void PushServiceConnect(string pushAppId)
115 PushImpl.Instance.PushServiceConnect(pushAppId);
119 /// API to disconnect from the push service.
121 /// <since_tizen> 3 </since_tizen>
122 public static void PushServiceDisconnect()
124 PushImpl.Instance.PushServiceDisconnect();
130 /// API to Register the application with the push server.
132 /// <since_tizen> 3 </since_tizen>
134 /// The method returns a task which on completion with give a ServerResponse Object.
136 public static Task<ServerResponse> PushServerRegister()
138 return PushImpl.Instance.PushServerRegister();
142 /// API to Deregister the application from the push server.
144 /// <since_tizen> 3 </since_tizen>
146 /// The method returns a task which on completion with give a ServerResponse Object.
148 public static Task<ServerResponse> PushServerUnregister()
150 return PushImpl.Instance.PushServerUnregister();
154 /// Gets the unread notifications for the application.
156 /// <since_tizen> 3 </since_tizen>
157 public static void GetUnreadNotifications()
159 PushImpl.Instance.GetUnreadNotifications();
163 /// registration Id received from server. </summary>
164 /// <since_tizen> 3 </since_tizen>
166 /// It is the string which is the Id received from the server.
168 public static string GetRegistrationId()
170 return PushImpl.Instance.GetRegistrationId();
173 internal static void StateChange(PushConnectionStateEventArgs args)
175 if (_stateChanged != null)
176 _stateChanged(null, args);
179 internal static void Notify(PushMessageEventArgs args)
181 if (_notificationReceived != null)
182 _notificationReceived(null, args);
185 private static object _lock = new object();
186 private static event EventHandler<PushMessageEventArgs> _notificationReceived;
187 private static event EventHandler<PushConnectionStateEventArgs> _stateChanged;