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 /// <since_tizen> 3 </since_tizen>
26 public static class NotificationManager
29 /// Posts a new notification.
31 /// <param name="notification">Notification to post.</param>
32 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
33 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
34 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
37 /// Notification notification = new Notification
40 /// Content = "content",
41 /// Icon = "absolute icon path",
42 /// Tag = "first notification"
45 /// Notification.AccessorySet accessory = new Notification.AccessorySet
47 /// SoundOption = AccessoryOption.On,
50 /// notification.Accessory = accessory;
54 /// NotificationManager.Post(notification);
57 /// <privilege>http://tizen.org/privilege/notification</privilege>
58 /// <since_tizen> 3 </since_tizen>
59 public static void Post(Notification notification)
61 if (notification == null)
63 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
68 NotificationError ret = Interop.Notification.Post(notification.Handle);
69 if (ret != NotificationError.None)
71 throw NotificationErrorFactory.GetException(ret, "post notification failed");
74 int priv_id, group_id;
75 Interop.Notification.GetID(notification.Handle, out group_id, out priv_id);
76 notification.PrivID = priv_id;
80 /// Updates a posted notification.
82 /// <param name="notification">Notification to update.</param>
83 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
84 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
85 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
88 /// string tag = "first tag";
90 /// Notification notification = new Notification
93 /// Content = "content",
94 /// Icon = "absolute icon path",
98 /// Notification.AccessorySet accessory = new Notification.AccessorySet
100 /// LedOption = AccessoryOption.On,
101 /// VibrationOption = AccessoryOption.Custom,
102 /// VibrationPath = "vibration absolute path"
104 /// notification.Accessory = accessory;
106 /// NotificationManager.Post(notification);
110 /// Notification loadNotification = NotificationManager.Load(tag);
112 /// loadNotification.Progress = new ProgressType(ProgressCategory.Percent, 0.0. 100.0);
114 /// Thread thread = new Thread(new ParameterizedThreadStart(UpdateProgress));
115 /// thread.IsBackground = true;
116 /// thread.Start(notification);
120 /// static void UpdateProgress(Object obj)
122 /// Notification notification = (Notification)obj;
124 /// for (double current = 1.0; current <= 100.0; current = current + 1.0)
126 /// notification.Progress.ProgressCurrent = current;
127 /// NotificationManager.Update(notification);
128 /// Thread.Sleep(300);
133 /// <privilege>http://tizen.org/privilege/notification</privilege>
135 /// Post method should be called on the notification object.
137 /// <since_tizen> 3 </since_tizen>
138 public static void Update(Notification notification)
140 if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
142 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
146 NotificationError ret = Interop.Notification.Update(notification.Handle);
147 if (ret != NotificationError.None)
149 throw NotificationErrorFactory.GetException(ret, "update notification failed");
154 /// Deletes a posted notification.
156 /// <param name="notification">Notification to remove.</param>
157 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
158 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
159 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
162 /// Notification notification = new Notification
165 /// Content = "content",
166 /// Icon = "absolute icon path",
167 /// Tag = "first notification"
169 /// NotificationManager.Post(notification);
173 /// NotificationManager.Delete(notification);
176 /// <privilege>http://tizen.org/privilege/notification</privilege>
178 /// Post method should be called on the notification object.
180 /// <since_tizen> 3 </since_tizen>
181 public static void Delete(Notification notification)
183 if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
185 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
188 NotificationError ret = Interop.Notification.Delete(notification.Handle);
189 if (ret != NotificationError.None)
191 throw NotificationErrorFactory.GetException(ret, "delete notification failed");
196 /// Removes all posted notifications of calling application.
198 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
199 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
202 /// Notification firstNotification = new Notification
205 /// Content = "content",
206 /// Icon = "absolute icon path",
207 /// Tag = "first notification"
209 /// NotificationManager.Post(firstNotification);
211 /// Notification secondNotification = new Notification
214 /// Content = "content",
215 /// Icon = "absolute icon path",
216 /// Tag = "second notification"
218 /// NotificationManager.Post(secondNotification);
219 /// NotificationManager.DeleteAll();
222 /// <privilege>http://tizen.org/privilege/notification</privilege>
223 /// <since_tizen> 3 </since_tizen>
224 public static void DeleteAll()
226 NotificationError ret;
228 ret = Interop.Notification.DeleteAll((int)NotificationType.Basic);
229 if (ret != NotificationError.None)
231 throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Noti type");
234 ret = Interop.Notification.DeleteAll((int)NotificationType.Ongoing);
235 if (ret != NotificationError.None)
237 throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Ongoing type");
242 /// Searches for a posted notification which has the specified tag and has not been deleted yet.
245 /// Load method should be called only for notifications, which have been posted using the NotificationManager.Post method.
246 /// If two or more notifications share the same tag, the notification posted most recently is returned.
248 /// <param name="tag">Tag used to query.</param>
249 /// <returns>Notification Object with specified tag.</returns>
250 /// <exception cref="ArgumentException">Throwing the same exception when argument is invalid and when the tag does not exist is misleading.</exception>
251 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
252 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
255 /// Notification notification = new Notification
258 /// Content = "content",
259 /// Icon = "absolute icon path",
260 /// Tag = "first notification"
262 /// NotificationManager.Post(notification);
266 /// Notification loadNotification = NotificationManager.Load("first notification");
269 /// <privilege>http://tizen.org/privilege/notification</privilege>
270 /// <since_tizen> 3 </since_tizen>
271 public static Notification Load(string tag)
273 IntPtr ptr = IntPtr.Zero;
275 if (string.IsNullOrEmpty(tag))
277 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
280 ptr = Interop.Notification.Load(tag);
282 if (ptr == IntPtr.Zero)
284 NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
285 Log.Error(Notification.LogTag, "unable to load Notification : " + ret.ToString());
286 if (ret == NotificationError.DbError)
288 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the tag does not exist");
292 throw NotificationErrorFactory.GetException(ret, "unable to load Notification");
296 Notification notification = new Notification
298 Handle = new NotificationSafeHandle(ptr, true)
305 /// Saves a notification template to the notification database.
307 /// <param name="notification">Notification to save as template.</param>
308 /// <param name="name">Template name.</param>
309 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
310 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
311 /// <exception cref="InvalidOperationException">Thrown when it can't be saved as a template.</exception>
314 /// Notification notification = new Notification
317 /// Content = "content",
318 /// Icon = "absolute icon path",
319 /// Tag = "first notification"
322 /// Notification.Accessory accessory = new Notification.Accessory
324 /// LedOption = AccessoryOption.On,
325 /// VibrationOption = AccessoryOption.Custom,
326 /// VibrationPath = "vibration absolute path"
328 /// notification.setAccessory(accessory);
332 /// NotificationManager.Post(notification);
334 /// Notification.LockStyle style = new Notification.LockStyle
336 /// IconPath = "icon path",
337 /// ThumbnailPath = "Thumbnail path"
339 /// notification.AddStyle(style);
340 /// NotificationManager.SaveTemplate(notification, "firstTemplate");
343 /// <privilege>http://tizen.org/privilege/notification</privilege>
344 /// <since_tizen> 3 </since_tizen>
345 public static void SaveTemplate(Notification notification, string name)
347 if (notification == null || string.IsNullOrEmpty(name))
349 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to save template");
354 NotificationError ret = Interop.Notification.SaveTemplate(notification.Handle, name);
355 if (ret != NotificationError.None)
357 throw NotificationErrorFactory.GetException(ret, "save as template failed");
362 /// Loads a notification template from the notification database.
364 /// <param name="name">Template name.</param>
365 /// <returns>Notification Object with inputted template name.</returns>
366 /// <exception cref="ArgumentException">Throwing the same exception when argument is invalid and when the template does not exist is misleading.</exception>
367 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
368 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
371 /// Notification notification = new Notification
374 /// Content = "content",
375 /// Icon = "absolute icon path",
376 /// Tag = "first notification"
379 /// Notification.Accessory accessory = new Notification.Accessory
381 /// LedOption = AccessoryOption.On,
382 /// VibrationOption = AccessoryOption.Custom,
383 /// VibrationPath = "vibration absolute path"
385 /// notification.setAccessory(accessory);
389 /// NotificationManager.Post(notification);
391 /// Notification.LockStyle style = new Notification.LockStyle
393 /// IconPath = "icon path",
394 /// ThumbnailPath = "Thumbnail path"
396 /// notification.AddStyle(style);
397 /// NotificationManager.SaveTemplate(notification, "firstTemplate");
398 /// Notification notificationTemplate = NotificationManager.LoadTemplate("firstTemplate");
401 /// <privilege>http://tizen.org/privilege/notification</privilege>
402 /// <since_tizen> 3 </since_tizen>
403 public static Notification LoadTemplate(string name)
405 IntPtr handle = IntPtr.Zero;
407 if (string.IsNullOrEmpty(name))
409 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to load template");
412 handle = Interop.Notification.LoadTemplate(name);
413 if (handle == IntPtr.Zero)
415 NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
416 if (ret == NotificationError.DbError)
418 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the name does not exist");
422 throw NotificationErrorFactory.GetException(ret, "unable to create Notification from template");
426 Notification notification = new Notification
428 Handle = new NotificationSafeHandle(handle, true)
435 /// Gets notification block state.
438 /// The user can set the notification block state in settings.
439 /// The block state indicates whether or not notifications can be posted.
440 /// Additionally, only notifications to the notification panel are allowed in "Do not disturb mode".
441 /// Sound, vibrate, and active notifications are blocked.
443 /// <returns>NotificationBlockState is a state if notification is posted.</returns>
444 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
445 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
446 /// <privilege>http://tizen.org/privilege/notification</privilege>
447 /// <since_tizen> 3 </since_tizen>
448 public static NotificationBlockState GetBlockState()
450 NotificationBlockState state;
451 NotificationError ret;
453 ret = Interop.Notification.GetBlockState(out state);
454 if (ret != NotificationError.None)
456 throw NotificationErrorFactory.GetException(ret, "GetBlockState failed");
459 Log.Info(Notification.LogTag, "Current block state is " + state.ToString());
464 /// Make a NotificationSafeHandle from Notification.
466 /// <param name="notification">The Notification class.</param>
467 /// <returns>The NotificationSafeHandle class.</returns>
468 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
469 /// <since_tizen> 3 </since_tizen>
470 [EditorBrowsable(EditorBrowsableState.Never)]
471 public static NotificationSafeHandle MakeNotificationSafeHandle(Notification notification)
473 if (notification == null)
475 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid notification object");
480 return notification.Handle;
484 /// Make a Notification from NotificationSafeHandle.
486 /// <param name="handle">The NotificationSafeHandle class.</param>
487 /// <returns>The Notification class.</returns>
488 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
489 /// <since_tizen> 3 </since_tizen>
490 [EditorBrowsable(EditorBrowsableState.Never)]
491 public static Notification MakeNotification(NotificationSafeHandle handle)
493 if (handle == null || handle.IsInvalid == true)
495 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "handle is invalid or null");
498 Notification notification = new Notification { Handle = handle }.Build();