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.Push.LogTag, "Creating New Instance");
23 _instance = new PushImpl();
35 private IntPtr _connection;
37 internal void PushServiceConnect(string pushAppId)
39 Interop.Push.VoidStateChangedCallback stateDelegate = (int state, string err, IntPtr userData) =>
41 PushConnectionStateEventArgs args = new PushConnectionStateEventArgs((PushConnectionStateEventArgs.PushState)state, err);
42 Push.StateChange(args);
44 Interop.Push.VoidNotifyCallback notifyDelegate = (IntPtr notification, IntPtr userData) =>
46 Interop.Push.ServiceError result;
47 PushMessageEventArgs ob = new PushMessageEventArgs();
49 result = Interop.Push.GetNotificationData(notification, out data);
50 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(data)))
59 result = Interop.Push.GetNotificationMessage(notification, out message);
60 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(message)))
69 result = Interop.Push.GetNotificationSender(notification, out sender);
70 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(sender)))
79 result = Interop.Push.GetNotificationSessionInfo(notification, out sessioninfo);
80 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(sessioninfo)))
82 ob.SessionInfo = sessioninfo;
89 result = Interop.Push.GetNotificationRequestId(notification, out requestid);
90 if ((result == Interop.Push.ServiceError.None) && !(String.IsNullOrEmpty(requestid)))
92 ob.RequestId = requestid;
99 result = Interop.Push.GetNotificationTime(notification, out time);
101 if ((result == Interop.Push.ServiceError.None) && (time != 0))
103 Log.Info(Interop.Push.LogTag, "Ticks received: " + time);
104 utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
105 ob.ReceivedAt = utc.ToLocalTime();
109 Log.Info(Interop.Push.LogTag, "No Date received");
110 ob.ReceivedAt = DateTime.Now;
113 result = Interop.Push.GetNotificationType(notification, out type);
114 if (result == Interop.Push.ServiceError.None)
119 //Interop.Push.FreeNotification(notification);
120 Log.Info(Interop.Push.LogTag, "Free Notification Done");
122 Interop.Push.ServiceError connectResult = Interop.Push.ServiceConnect(pushAppId, stateDelegate, notifyDelegate, IntPtr.Zero, out _connection);
123 if (connectResult != Interop.Push.ServiceError.None)
125 Log.Error(Interop.Push.LogTag, "Connect failed with "+ connectResult);
126 throw PushExceptionFactory.CreateResponseException(connectResult);
130 internal void PushServiceDisconnect()
132 Interop.Push.ServiceDisconnect(_connection);
133 Log.Info(Interop.Push.LogTag, "PushServiceDisconnect Completed");
136 internal async Task<ServerResponse> PushServerRegister()
138 Log.Info(Interop.Push.LogTag, "Register Called");
139 var task = new TaskCompletionSource<ServerResponse>();
140 Interop.Push.VoidResultCallback registerResult = (Interop.Push.Result regResult, IntPtr msgPtr, IntPtr userData) =>
142 Log.Info(Interop.Push.LogTag, "Register Callback Called");
144 if (msgPtr != IntPtr.Zero)
146 msg = Marshal.PtrToStringAnsi(msgPtr);
148 ServerResponse response = new ServerResponse();
149 response.ServerResult = (ServerResponse.Result)regResult;
150 response.ServerMessage = msg;
151 if (task.TrySetResult(response) == false)
153 Log.Error(Interop.Push.LogTag, "Unable to set the Result for register");
156 Interop.Push.ServiceError result = Interop.Push.ServiceRegister(_connection, registerResult, IntPtr.Zero);
157 Log.Info(Interop.Push.LogTag, "Interop.Push.ServiceRegister Completed");
158 if (result != Interop.Push.ServiceError.None)
160 Log.Error(Interop.Push.LogTag, "Register failed with " + result);
161 task.SetException(PushExceptionFactory.CreateResponseException(result));
163 return await task.Task;
166 internal async Task<ServerResponse> PushServerUnregister()
168 var task = new TaskCompletionSource<ServerResponse>();
169 Interop.Push.VoidResultCallback registerResult = (Interop.Push.Result regResult, IntPtr msgPtr, IntPtr userData) =>
171 Log.Info(Interop.Push.LogTag, "Unregister Callback Called");
173 if (msgPtr != IntPtr.Zero)
175 msg = Marshal.PtrToStringAnsi(msgPtr);
177 ServerResponse response = new ServerResponse();
178 response.ServerResult = (ServerResponse.Result)regResult;
179 response.ServerMessage = msg;
180 if (task.TrySetResult(response) == false)
182 Log.Error(Interop.Push.LogTag, "Unable to set the Result for Unregister");
185 Interop.Push.ServiceError result = Interop.Push.ServiceDeregister(_connection, registerResult, IntPtr.Zero);
186 if (result != Interop.Push.ServiceError.None)
188 task.SetException(PushExceptionFactory.CreateResponseException(result));
190 return await task.Task;
193 internal string GetRegistrationId()
196 Interop.Push.ServiceError result = Interop.Push.GetRegistrationId(_connection, out regID);
197 if (result != Interop.Push.ServiceError.None)
199 throw PushExceptionFactory.CreateResponseException(result);
201 Log.Info(Interop.Push.LogTag, "Returning Reg Id: " + regID);
205 internal void GetUnreadNotifications()
207 Interop.Push.ServiceError result = Interop.Push.RequestUnreadNotification(_connection);
208 if (result != Interop.Push.ServiceError.None)
210 throw PushExceptionFactory.CreateResponseException(result);
214 internal static void Reset()
218 Log.Info(Interop.Push.LogTag, "Making _instance as null");