Adding Null Check for strings
[platform/core/csapi/push.git] / Tizen.Messaging / Push / Push.cs
1 /// This File contains the Api's related to the PushManager class
2 ///
3 /// Copyright 2016 by Samsung Electronics, Inc.,
4 ///
5 /// This software is the confidential and proprietary information
6 /// of Samsung Electronics, Inc. ("Confidential Information"). You
7 /// shall not disclose such Confidential Information and shall use
8 /// it only in accordance with the terms of the license agreement
9 /// you entered into with Samsung.
10
11
12 using System;
13 using System.Threading.Tasks;
14
15 namespace Tizen.Messaging.Push
16 {
17     /// <summary>
18     /// The PushManager API provides functions to connect to push service for receiving push messages.
19     /// </summary>
20     /// <remarks>
21     /// The PushManager API provides the way to connect with the push service.
22     /// It provides api's to connect/disconnect from the push service.
23     /// Api's are provided so that an application can register itself
24     /// with the push server along with api's to request push message.
25     /// </remarks>
26     /// <example>
27     /// <code>
28     /// public class Program
29     /// {
30     ///     static void Main(string[] args)
31     ///     {
32     ///         Push.PushServiceConnect("xxxxx");
33     ///         Task<ServerResponse> tr = Push.PushServerRegister();
34     ///         tr.GetAwaiter().OnCompleted(() => {
35     ///             ServerResponse res = tr.Result;
36     ///             Push.GetUnreadNotifications();
37     ///             Task<ServerResponse> tu = Push.PushServerUnregister();
38     ///             tu.GetAwaiter().OnCompleted(() => {
39     ///                 Push.PushServiceDisconnect();
40     ///             });
41     ///         });
42     ///         Push.NotificationReceived += EventHandlerNotificationReceived;
43     ///         Push.StateChanged += EventHandlerStateChanged;
44     ///     }
45     /// }
46     /// static void EventHandlerNotificationReceived(object sender, PushMessageEventArgs e)
47     /// {
48     ///     // any user code
49     /// }
50     /// static void EventHandlerStateChanged(object sender, PushConnectionStateEventArgs e)
51     /// {
52     ///     // any user code
53     /// }
54     /// </code>
55     /// </example>
56     public static class Push
57     {
58         /// <summary>
59         /// Event Handler for receiving the notifications.
60         /// </summary>
61         public static event EventHandler<PushMessageEventArgs> NotificationReceived
62         {
63             add
64             {
65                 lock (_lock)
66                 {
67                     _notificationReceived += value;
68                 }
69             }
70             remove
71             {
72                 lock (_lock)
73                 {
74                     _notificationReceived -= value;
75                 }
76             }
77         }
78
79         /// <summary>
80         /// Event Handler for receiving changes in States of the connection.
81         /// </summary>
82         public static event EventHandler<PushConnectionStateEventArgs> StateChanged
83         {
84             add
85             {
86                 lock (_lock)
87                 {
88                     _stateChanged += value;
89                 }
90             }
91             remove
92             {
93                 lock (_lock)
94                 {
95                     _stateChanged -= value;
96                 }
97             }
98         }
99
100         /// <summary>
101         /// API to connect with the push service.
102         /// </summary>
103         /// <param name="pushAppId"> The Push Application Id Registered with the server.</param>
104         public static void PushServiceConnect(string pushAppId)
105         {
106             PushImpl.Instance.PushServiceConnect(pushAppId);
107         }
108
109         /// <summary>
110         /// API to disconnect from the push service.
111         /// </summary>
112         public static void PushServiceDisconnect()
113         {
114             PushImpl.Instance.PushServiceDisconnect();
115             PushImpl.Reset();
116         }
117
118
119         /// <summary>
120         /// API to Register the application with the push server.
121         /// </summary>
122         /// <returns>
123         /// The method returns a task which on completion with give a ServerResponse Object.
124         /// </returns>
125         public static Task<ServerResponse> PushServerRegister()
126         {
127             return PushImpl.Instance.PushServerRegister();
128         }
129
130         /// <summary>
131         /// API to Deregister the application from the push server.
132         /// </summary>
133         /// <returns>
134         /// The method returns a task which on completion with give a ServerResponse Object.
135         /// </returns>
136         public static Task<ServerResponse> PushServerUnregister()
137         {
138             return PushImpl.Instance.PushServerUnregister();
139         }
140
141         /// <summary>
142         /// Gets the unread notifications for the application.
143         /// </summary>
144         public static void GetUnreadNotifications()
145         {
146             PushImpl.Instance.GetUnreadNotifications();
147         }
148
149         /// <summary>
150         /// registration Id received from server. </summary>
151         /// <returns>
152         /// It is the string which is the Id received from the server.
153         /// </returns>
154         public static string GetRegistrationId()
155         {
156             return PushImpl.Instance.GetRegistrationId();
157         }
158
159         internal static void StateChange(PushConnectionStateEventArgs args)
160         {
161             if (_stateChanged != null)
162                 _stateChanged(null, args);
163         }
164
165         internal static void Notify(PushMessageEventArgs args)
166         {
167             if (_notificationReceived != null)
168                 _notificationReceived(null, args);
169         }
170
171         private static object _lock = new object();
172         private static event EventHandler<PushMessageEventArgs> _notificationReceived;
173         private static event EventHandler<PushConnectionStateEventArgs> _stateChanged;
174     }
175 }