2 using System.Collections.Generic;
3 using System.Threading.Tasks;
4 using System.Runtime.InteropServices;
7 namespace Tizen.Messaging.Push
9 internal class PushImpl
11 private static readonly object _lock = new object();
12 private static PushImpl _instance;
14 internal static PushImpl Instance
20 if (_instance == null)
22 Log.Info(Interop.PushClient.LogTag, "Creating New Instance");
23 _instance = new PushImpl();
35 private IntPtr _connection;
37 internal void PushServiceConnect(string pushAppId)
39 Interop.PushClient.VoidStateChangedCallback stateDelegate = (int state, string err, IntPtr userData) =>
45 PushConnectionStateEventArgs args = new PushConnectionStateEventArgs((PushConnectionStateEventArgs.PushState)state, err);
46 PushClient.StateChange(args);
48 Interop.Push.VoidNotifyCallback notifyDelegate = (IntPtr notification, IntPtr userData) =>
50 Interop.PushClient.ServiceError result;
51 PushMessageEventArgs ob = new PushMessageEventArgs();
53 result = Interop.PushClient.GetNotificationData(notification, out data);
54 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(data)))
63 result = Interop.PushClient.GetNotificationMessage(notification, out message);
64 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(message)))
73 result = Interop.PushClient.GetNotificationSender(notification, out sender);
74 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(sender)))
83 result = Interop.PushClient.GetNotificationSessionInfo(notification, out sessioninfo);
84 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(sessioninfo)))
86 ob.SessionInfo = sessioninfo;
93 result = Interop.PushClient.GetNotificationRequestId(notification, out requestid);
94 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(requestid)))
96 ob.RequestId = requestid;
103 result = Interop.PushClient.GetNotificationTime(notification, out time);
105 if ((result == Interop.PushClient.ServiceError.None) && (time != 0))
107 Log.Info(Interop.PushClient.LogTag, "Ticks received: " + time);
108 utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
109 ob.ReceivedAt = utc.ToLocalTime();
113 Log.Info(Interop.PushClient.LogTag, "No Date received");
114 ob.ReceivedAt = DateTime.Now;
117 result = Interop.PushClient.GetNotificationType(notification, out type);
118 if (result == Interop.PushClient.ServiceError.None)
122 PushClient.Notify(ob);
123 //Interop.Push.FreeNotification(notification);
124 Log.Info(Interop.PushClient.LogTag, "Free Notification Done");
126 Interop.PushClient.ServiceError connectResult = Interop.Push.ServiceConnect(pushAppId, stateDelegate, notifyDelegate, IntPtr.Zero, out _connection);
127 if (connectResult != Interop.PushClient.ServiceError.None)
129 Log.Error(Interop.PushClient.LogTag, "Connect failed with " + connectResult);
130 throw PushExceptionFactory.CreateResponseException(connectResult);
134 internal void PushServiceDisconnect()
136 Interop.PushClient.ServiceDisconnect(_connection);
137 Log.Info(Interop.PushClient.LogTag, "PushServiceDisconnect Completed");
140 internal async Task<ServerResponse> PushServerRegister()
142 Log.Info(Interop.PushClient.LogTag, "Register Called");
143 var task = new TaskCompletionSource<ServerResponse>();
144 Interop.PushClient.VoidResultCallback registerResult = (Interop.PushClient.Result regResult, IntPtr msgPtr, IntPtr userData) =>
146 Log.Info(Interop.PushClient.LogTag, "Register Callback Called");
148 if (msgPtr != IntPtr.Zero)
150 msg = Marshal.PtrToStringAnsi(msgPtr);
152 ServerResponse response = new ServerResponse();
153 response.ServerResult = (ServerResponse.Result)regResult;
154 response.ServerMessage = msg;
155 if (task.TrySetResult(response) == false)
157 Log.Error(Interop.PushClient.LogTag, "Unable to set the Result for register");
160 Interop.PushClient.ServiceError result = Interop.PushClient.ServiceRegister(_connection, registerResult, IntPtr.Zero);
161 Log.Info(Interop.PushClient.LogTag, "Interop.Push.ServiceRegister Completed");
162 if (result != Interop.PushClient.ServiceError.None)
164 Log.Error(Interop.PushClient.LogTag, "Register failed with " + result);
165 task.SetException(PushExceptionFactory.CreateResponseException(result));
167 return await task.Task;
170 internal async Task<ServerResponse> PushServerUnregister()
172 var task = new TaskCompletionSource<ServerResponse>();
173 Interop.PushClient.VoidResultCallback registerResult = (Interop.PushClient.Result regResult, IntPtr msgPtr, IntPtr userData) =>
175 Log.Info(Interop.PushClient.LogTag, "Unregister Callback Called");
177 if (msgPtr != IntPtr.Zero)
179 msg = Marshal.PtrToStringAnsi(msgPtr);
181 ServerResponse response = new ServerResponse();
182 response.ServerResult = (ServerResponse.Result)regResult;
183 response.ServerMessage = msg;
184 if (task.TrySetResult(response) == false)
186 Log.Error(Interop.PushClient.LogTag, "Unable to set the Result for Unregister");
189 Interop.PushClient.ServiceError result = Interop.PushClient.ServiceDeregister(_connection, registerResult, IntPtr.Zero);
190 if (result != Interop.PushClient.ServiceError.None)
192 task.SetException(PushExceptionFactory.CreateResponseException(result));
194 return await task.Task;
197 internal string GetRegistrationId()
200 Interop.PushClient.ServiceError result = Interop.PushClient.GetRegistrationId(_connection, out regID);
201 if (result != Interop.PushClient.ServiceError.None)
203 throw PushExceptionFactory.CreateResponseException(result);
205 Log.Info(Interop.PushClient.LogTag, "Returning Reg Id: " + regID);
209 internal void GetUnreadNotifications()
211 Interop.PushClient.ServiceError result = Interop.PushClient.RequestUnreadNotification(_connection);
212 if (result != Interop.PushClient.ServiceError.None)
214 throw PushExceptionFactory.CreateResponseException(result);
218 internal static void Reset()
222 Log.Info(Interop.PushClient.LogTag, "Making _instance as null");