2 * Copyright (c) 2017 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.
17 namespace Tizen.Applications.Notifications
20 using System.ComponentModel;
23 /// NotificationManager class to post, update, delete and get Notification.
25 public static class NotificationManager
28 /// Posts a new Notification.
30 /// <param name="notification">Notification to post</param>
31 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
32 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
33 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
36 /// Notification notification = new Notification
39 /// Content = "content",
40 /// Icon = "absolute icon path",
41 /// Tag = "first notification"
44 /// Notification.AccessorySet accessory = new Notification.AccessorySet
46 /// SoundOption = AccessoryOption.On,
49 /// notification.Accessory = accessory;
53 /// NotificationManager.Post(notification);
56 /// <privilege>http://tizen.org/privilege/notification</privilege>
57 public static void Post(Notification notification)
59 if (notification == null)
61 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
66 NotificationError ret = Interop.Notification.Post(notification.Handle);
67 if (ret != NotificationError.None)
69 throw NotificationErrorFactory.GetException(ret, "post notification failed");
72 int priv_id, group_id;
73 Interop.Notification.GetID(notification.Handle, out group_id, out priv_id);
74 notification.PrivID = priv_id;
78 /// Updates a posted Notification.
80 /// <param name="notification">Notification to update</param>
81 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
82 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
83 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
86 /// string tag = "first tag";
88 /// Notification notification = new Notification
91 /// Content = "content",
92 /// Icon = "absolute icon path",
96 /// Notification.AccessorySet accessory = new Notification.AccessorySet
98 /// LedOption = AccessoryOption.On,
99 /// VibrationOption = AccessoryOption.Custom,
100 /// VibrationPath = "vibration absolute path"
102 /// notification.Accessory = accessory;
104 /// NotificationManager.Post(notification);
108 /// Notification loadNotification = NotificationManager.Load(tag);
110 /// loadNotification.Progress = new ProgressType(ProgressCategory.Percent, 0.0. 100.0);
112 /// Thread thread = new Thread(new ParameterizedThreadStart(UpdateProgress));
113 /// thread.IsBackground = true;
114 /// thread.Start(notification);
118 /// static void UpdateProgress(Object obj)
120 /// Notification notification = (Notification)obj;
122 /// for (double current = 1.0; current <= 100.0; current = current + 1.0)
124 /// notification.Progress.ProgressCurrent = current;
125 /// NotificationManager.Update(notification);
126 /// Thread.Sleep(300);
131 /// <privilege>http://tizen.org/privilege/notification</privilege>
133 /// Post method should be called on the Notification object.
135 public static void Update(Notification notification)
137 if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
139 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
143 NotificationError ret = Interop.Notification.Update(notification.Handle);
144 if (ret != NotificationError.None)
146 throw NotificationErrorFactory.GetException(ret, "update notification failed");
151 /// Deletes a posted Notification.
153 /// <param name="notification">Notification to remove</param>
154 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
155 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
156 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
159 /// Notification notification = new Notification
162 /// Content = "content",
163 /// Icon = "absolute icon path",
164 /// Tag = "first notification"
166 /// NotificationManager.Post(notification);
170 /// NotificationManager.Delete(notification);
173 /// <privilege>http://tizen.org/privilege/notification</privilege>
175 /// Post method should be called on the Notification object.
177 public static void Delete(Notification notification)
179 if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
181 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
184 NotificationError ret = Interop.Notification.Delete(notification.Handle);
185 if (ret != NotificationError.None)
187 throw NotificationErrorFactory.GetException(ret, "delete notification failed");
192 /// Removes all posted Notification of calling application.
194 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
195 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
198 /// Notification firstNotification = new Notification
201 /// Content = "content",
202 /// Icon = "absolute icon path",
203 /// Tag = "first notification"
205 /// NotificationManager.Post(firstNotification);
207 /// Notification secondNotification = new Notification
210 /// Content = "content",
211 /// Icon = "absolute icon path",
212 /// Tag = "second notification"
214 /// NotificationManager.Post(secondNotification);
215 /// NotificationManager.DeleteAll();
218 /// <privilege>http://tizen.org/privilege/notification</privilege>
219 public static void DeleteAll()
221 NotificationError ret;
223 ret = Interop.Notification.DeleteAll((int)NotificationType.Basic);
224 if (ret != NotificationError.None)
226 throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Noti type");
229 ret = Interop.Notification.DeleteAll((int)NotificationType.Ongoing);
230 if (ret != NotificationError.None)
232 throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Ongoing type");
237 /// Searches for a posted notification which has the inputted tag and isn't deleted not yet.
240 /// Load method should be called only for notifications which have been posted using NotificationManager.Post method.
241 /// If two or more notifications share the same tag, the notification posted most recently is returned.
243 /// <param name="tag">Tag used to query</param>
244 /// <returns>Notification Object with inputted tag</returns>
245 /// <exception cref="ArgumentException">Thrown when argument is invalid or when the tag does not exist</exception>
246 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
247 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
250 /// Notification notification = new Notification
253 /// Content = "content",
254 /// Icon = "absolute icon path",
255 /// Tag = "first notification"
257 /// NotificationManager.Post(notification);
261 /// Notification loadNotification = NotificationManager.Load("first notification");
264 /// <privilege>http://tizen.org/privilege/notification</privilege>
265 public static Notification Load(string tag)
267 IntPtr ptr = IntPtr.Zero;
269 if (string.IsNullOrEmpty(tag))
271 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
274 ptr = Interop.Notification.Load(tag);
276 if (ptr == IntPtr.Zero)
278 NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
279 Log.Error(Notification.LogTag, "unable to load Notification : " + ret.ToString());
280 if (ret == NotificationError.DbError)
282 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the tag does not exist");
286 throw NotificationErrorFactory.GetException(ret, "unable to load Notification");
290 Notification notification = new Notification
292 Handle = new NotificationSafeHandle(ptr, true)
299 /// Saves a notification template to the notification database
301 /// <param name="notification">Notification to save as template</param>
302 /// <param name="name">Template name</param>
303 /// <exception cref="ArgumentException">Thrown when argument is invalid</exception>
304 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
305 /// <exception cref="InvalidOperationException">Thrown when can't save as template</exception>
308 /// Notification notification = new Notification
311 /// Content = "content",
312 /// Icon = "absolute icon path",
313 /// Tag = "first notification"
316 /// Notification.Accessory accessory = new Notification.Accessory
318 /// LedOption = AccessoryOption.On,
319 /// VibrationOption = AccessoryOption.Custom,
320 /// VibrationPath = "vibration absolute path"
322 /// notification.setAccessory(accessory);
326 /// NotificationManager.Post(notification);
328 /// Notification.LockStyle style = new Notification.LockStyle
330 /// IconPath = "icon path",
331 /// ThumbnailPath = "Thumbnail path"
333 /// notification.AddStyle(style);
334 /// NotificationManager.SaveTemplate(notification, "firstTemplate");
337 /// <privilege>http://tizen.org/privilege/notification</privilege>
338 public static void SaveTemplate(Notification notification, string name)
340 if (notification == null || string.IsNullOrEmpty(name))
342 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to save template");
347 NotificationError ret = Interop.Notification.SaveTemplate(notification.Handle, name);
348 if (ret != NotificationError.None)
350 throw NotificationErrorFactory.GetException(ret, "save as template failed");
355 /// Loads a notification template from the notification database
357 /// <param name="name">Template name</param>
358 /// <returns>Notification Object with inputted template name</returns>
359 /// <exception cref="ArgumentException">Thrown when argument is invalid or when no template with input name exists</exception>
360 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
361 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
364 /// Notification notification = new Notification
367 /// Content = "content",
368 /// Icon = "absolute icon path",
369 /// Tag = "first notification"
372 /// Notification.Accessory accessory = new Notification.Accessory
374 /// LedOption = AccessoryOption.On,
375 /// VibrationOption = AccessoryOption.Custom,
376 /// VibrationPath = "vibration absolute path"
378 /// notification.setAccessory(accessory);
382 /// NotificationManager.Post(notification);
384 /// Notification.LockStyle style = new Notification.LockStyle
386 /// IconPath = "icon path",
387 /// ThumbnailPath = "Thumbnail path"
389 /// notification.AddStyle(style);
390 /// NotificationManager.SaveTemplate(notification, "firstTemplate");
391 /// Notification notificationTemplate = NotificationManager.LoadTemplate("firstTemplate");
394 /// <privilege>http://tizen.org/privilege/notification</privilege>
395 public static Notification LoadTemplate(string name)
397 IntPtr handle = IntPtr.Zero;
399 if (string.IsNullOrEmpty(name))
401 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to load template");
404 handle = Interop.Notification.LoadTemplate(name);
405 if (handle == IntPtr.Zero)
407 NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
408 if (ret == NotificationError.DbError)
410 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the name does not exist");
414 throw NotificationErrorFactory.GetException(ret, "unable to create Notification from template");
418 Notification notification = new Notification
420 Handle = new NotificationSafeHandle(handle, true)
427 /// Gets notification block state.
430 /// The user can set the notification block state in settings.
431 /// The block state indicates whether or not notifications can be posted.
432 /// Additionally only notifications to the notification panel are allowed in "Do not disturb mode".
433 /// Sound, Vibrate and Active notifications are blocked.
435 /// <returns>NotificationBlockState is state if notification is posted</returns>
436 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
437 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
438 /// <privilege>http://tizen.org/privilege/notification</privilege>
439 public static NotificationBlockState GetBlockState()
441 NotificationBlockState state;
442 NotificationError ret;
444 ret = Interop.Notification.GetBlockState(out state);
445 if (ret != NotificationError.None)
447 throw NotificationErrorFactory.GetException(ret, "GetBlockState failed");
450 Log.Info(Notification.LogTag, "Current block state is " + state.ToString());
454 [EditorBrowsable(EditorBrowsableState.Never)]
455 public static NotificationSafeHandle MakeNotificationSafeHandle(Notification notification)
457 if (notification == null)
459 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid notification object");
464 return notification.Handle;
467 [EditorBrowsable(EditorBrowsableState.Never)]
468 public static Notification MakeNotification(NotificationSafeHandle handle)
470 if (handle == null || handle.IsInvalid == true)
472 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "handle is invalid or null");
475 Notification notification = new Notification { Handle = handle }.Build();