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.Collections.Generic;
19 using System.Threading.Tasks;
20 using System.Runtime.InteropServices;
23 namespace Tizen.Messaging.Push
25 internal class PushImpl
27 private static readonly object _lock = new object();
28 private static PushImpl _instance;
29 private Interop.PushClient.VoidResultCallback registerResult;
30 private Interop.PushClient.VoidResultCallback unregisterResult;
32 internal static PushImpl Instance
38 if (_instance == null)
40 Log.Info(Interop.PushClient.LogTag, "Creating New Instance");
41 _instance = new PushImpl();
53 private IntPtr _connection;
55 internal void PushServiceConnect(string pushAppId)
57 Interop.PushClient.VoidStateChangedCallback stateDelegate = (int state, string err, IntPtr userData) =>
63 PushConnectionStateEventArgs args = new PushConnectionStateEventArgs((PushConnectionStateEventArgs.PushState)state, err);
64 PushClient.StateChange(args);
66 Interop.PushClient.VoidNotifyCallback notifyDelegate = (IntPtr notification, IntPtr userData) =>
68 Interop.PushClient.ServiceError result;
69 PushMessageEventArgs ob = new PushMessageEventArgs();
71 result = Interop.PushClient.GetNotificationData(notification, out data);
72 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(data)))
81 result = Interop.PushClient.GetNotificationMessage(notification, out message);
82 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(message)))
91 result = Interop.PushClient.GetNotificationSender(notification, out sender);
92 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(sender)))
101 result = Interop.PushClient.GetNotificationSessionInfo(notification, out sessioninfo);
102 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(sessioninfo)))
104 ob.SessionInfo = sessioninfo;
111 result = Interop.PushClient.GetNotificationRequestId(notification, out requestid);
112 if ((result == Interop.PushClient.ServiceError.None) && !(String.IsNullOrEmpty(requestid)))
114 ob.RequestId = requestid;
121 result = Interop.PushClient.GetNotificationTime(notification, out time);
123 if ((result == Interop.PushClient.ServiceError.None) && (time != 0))
125 Log.Info(Interop.PushClient.LogTag, "Ticks received: " + time);
126 utc = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(time), DateTimeKind.Utc);
127 ob.ReceivedAt = utc.ToLocalTime();
131 Log.Info(Interop.PushClient.LogTag, "No Date received");
132 ob.ReceivedAt = DateTime.Now;
135 result = Interop.PushClient.GetNotificationType(notification, out type);
136 if (result == Interop.PushClient.ServiceError.None)
140 PushClient.Notify(ob);
142 Interop.PushClient.ServiceError connectResult = Interop.PushClient.ServiceConnect(pushAppId, stateDelegate, notifyDelegate, IntPtr.Zero, out _connection);
143 if (connectResult != Interop.PushClient.ServiceError.None)
145 Log.Error(Interop.PushClient.LogTag, "Connect failed with " + connectResult);
146 throw PushExceptionFactory.CreateResponseException(connectResult);
150 internal void PushServiceDisconnect()
152 Interop.PushClient.ServiceDisconnect(_connection);
153 Log.Info(Interop.PushClient.LogTag, "PushServiceDisconnect Completed");
156 internal async Task<ServerResponse> PushServerRegister()
158 Log.Info(Interop.PushClient.LogTag, "Register Called");
159 var task = new TaskCompletionSource<ServerResponse>();
160 registerResult = (Interop.PushClient.Result regResult, IntPtr msgPtr, IntPtr userData) =>
162 Log.Info(Interop.PushClient.LogTag, "Register Callback Called with " + regResult);
164 if (msgPtr != IntPtr.Zero)
166 msg = Marshal.PtrToStringAnsi(msgPtr);
168 ServerResponse response = new ServerResponse();
169 response.ServerResult = (ServerResponse.Result)regResult;
170 response.ServerMessage = msg;
171 if (task.TrySetResult(response) == false)
173 Log.Error(Interop.PushClient.LogTag, "Unable to set the Result for register");
176 Interop.PushClient.ServiceError result = Interop.PushClient.ServiceRegister(_connection, registerResult, IntPtr.Zero);
177 Log.Info(Interop.PushClient.LogTag, "Interop.PushClient.ServiceRegister Completed");
178 if (result != Interop.PushClient.ServiceError.None)
180 Log.Error(Interop.PushClient.LogTag, "Register failed with " + result);
181 task.SetException(PushExceptionFactory.CreateResponseException(result));
183 return await task.Task;
186 internal async Task<ServerResponse> PushServerUnregister()
188 var task = new TaskCompletionSource<ServerResponse>();
189 unregisterResult = (Interop.PushClient.Result regResult, IntPtr msgPtr, IntPtr userData) =>
191 Log.Info(Interop.PushClient.LogTag, "Unregister Callback Called");
193 if (msgPtr != IntPtr.Zero)
195 msg = Marshal.PtrToStringAnsi(msgPtr);
197 ServerResponse response = new ServerResponse();
198 response.ServerResult = (ServerResponse.Result)regResult;
199 response.ServerMessage = msg;
200 if (task.TrySetResult(response) == false)
202 Log.Error(Interop.PushClient.LogTag, "Unable to set the Result for Unregister");
205 Interop.PushClient.ServiceError result = Interop.PushClient.ServiceDeregister(_connection, unregisterResult, IntPtr.Zero);
206 if (result != Interop.PushClient.ServiceError.None)
208 task.SetException(PushExceptionFactory.CreateResponseException(result));
210 return await task.Task;
213 internal string GetRegistrationId()
216 Interop.PushClient.ServiceError result = Interop.PushClient.GetRegistrationId(_connection, out regID);
217 if (result != Interop.PushClient.ServiceError.None)
219 throw PushExceptionFactory.CreateResponseException(result);
221 Log.Info(Interop.PushClient.LogTag, "Returning Reg Id: " + regID);
225 internal void GetUnreadNotifications()
227 Interop.PushClient.ServiceError result = Interop.PushClient.RequestUnreadNotification(_connection);
228 if (result != Interop.PushClient.ServiceError.None)
230 throw PushExceptionFactory.CreateResponseException(result);
234 internal static void Reset()
238 Log.Info(Interop.PushClient.LogTag, "Making _instance as null");