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
28 private static event EventHandler<NotificationResponseEventArgs> ResponseEventHandler;
30 private static Interop.Notification.ResponseEventCallback responseEventCallback;
32 private static void ResponseEventCallback(IntPtr ptr, int type, IntPtr userData)
35 NotificationError ret = Interop.Notification.Clone(ptr, out cloned);
36 if (ret != NotificationError.None)
38 Log.Error(Notification.LogTag, "Fail to clone notification : " + ret.ToString());
42 NotificationResponseEventArgs eventArgs = new NotificationResponseEventArgs();
43 eventArgs.EventType = (NotificationResponseEventType)type;
44 eventArgs.Notification = new Notification
46 Handle = new NotificationSafeHandle(cloned, true)
48 ResponseEventHandler?.Invoke(null, eventArgs);
52 /// The event handler for receiving a response event from the notification viewers
54 /// <since_tizen> 8 </since_tizen>
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public static event EventHandler<NotificationResponseEventArgs> ResponseReceived
60 if (responseEventCallback == null)
62 responseEventCallback = new Interop.Notification.ResponseEventCallback(ResponseEventCallback);
65 ResponseEventHandler += value;
70 if (ResponseEventHandler != null && ResponseEventHandler.GetInvocationList().Length > 0)
72 ResponseEventHandler -= value;
78 /// Posts a new notification.
80 /// <param name="notification">Notification to post.</param>
81 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
82 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
83 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
86 /// Notification notification = new Notification
89 /// Content = "content",
90 /// Icon = "absolute icon path",
91 /// Tag = "first notification"
94 /// Notification.AccessorySet accessory = new Notification.AccessorySet
96 /// SoundOption = AccessoryOption.On,
99 /// notification.Accessory = accessory;
103 /// NotificationManager.Post(notification);
106 /// <privilege>http://tizen.org/privilege/notification</privilege>
107 /// <since_tizen> 3 </since_tizen>
108 public static void Post(Notification notification)
110 if (notification == null)
112 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
117 if (ResponseEventHandler != null && ResponseEventHandler.GetInvocationList().Length > 0)
119 NotificationError ret = Interop.Notification.PostWithEventCallback(notification.Handle, responseEventCallback, IntPtr.Zero);
120 if (ret != NotificationError.None)
122 throw NotificationErrorFactory.GetException(ret, "post notification with event callback failed");
127 NotificationError ret = Interop.Notification.Post(notification.Handle);
128 if (ret != NotificationError.None)
130 throw NotificationErrorFactory.GetException(ret, "post notification failed");
134 int priv_id, group_id;
135 Interop.Notification.GetID(notification.Handle, out group_id, out priv_id);
136 notification.PrivID = priv_id;
140 /// Updates a posted notification.
142 /// <param name="notification">Notification to update.</param>
143 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
144 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
145 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
148 /// string tag = "first tag";
150 /// Notification notification = new Notification
153 /// Content = "content",
154 /// Icon = "absolute icon path",
158 /// Notification.AccessorySet accessory = new Notification.AccessorySet
160 /// LedOption = AccessoryOption.On,
161 /// VibrationOption = AccessoryOption.Custom,
162 /// VibrationPath = "vibration absolute path"
164 /// notification.Accessory = accessory;
166 /// NotificationManager.Post(notification);
170 /// Notification loadNotification = NotificationManager.Load(tag);
172 /// loadNotification.Progress = new ProgressType(ProgressCategory.Percent, 0.0. 100.0);
174 /// Thread thread = new Thread(new ParameterizedThreadStart(UpdateProgress));
175 /// thread.IsBackground = true;
176 /// thread.Start(notification);
180 /// static void UpdateProgress(Object obj)
182 /// Notification notification = (Notification)obj;
184 /// for (double current = 1.0; current <= 100.0; current = current + 1.0)
186 /// notification.Progress.ProgressCurrent = current;
187 /// NotificationManager.Update(notification);
188 /// Thread.Sleep(300);
193 /// <privilege>http://tizen.org/privilege/notification</privilege>
195 /// Post method should be called on the notification object.
197 /// <since_tizen> 3 </since_tizen>
198 public static void Update(Notification notification)
200 if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
202 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
206 NotificationError ret = Interop.Notification.Update(notification.Handle);
207 if (ret != NotificationError.None)
209 throw NotificationErrorFactory.GetException(ret, "update notification failed");
214 /// Deletes a posted notification.
216 /// <param name="notification">Notification to remove.</param>
217 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
218 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
219 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
222 /// Notification notification = new Notification
225 /// Content = "content",
226 /// Icon = "absolute icon path",
227 /// Tag = "first notification"
229 /// NotificationManager.Post(notification);
233 /// NotificationManager.Delete(notification);
236 /// <privilege>http://tizen.org/privilege/notification</privilege>
238 /// Post method should be called on the notification object.
240 /// <since_tizen> 3 </since_tizen>
241 public static void Delete(Notification notification)
243 if (notification == null || notification.Handle == null || notification.Handle.IsInvalid)
245 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to post method");
248 NotificationError ret = Interop.Notification.Delete(notification.Handle);
249 if (ret != NotificationError.None)
251 throw NotificationErrorFactory.GetException(ret, "delete notification failed");
256 /// Removes all posted notifications of calling application.
258 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
259 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
262 /// Notification firstNotification = new Notification
265 /// Content = "content",
266 /// Icon = "absolute icon path",
267 /// Tag = "first notification"
269 /// NotificationManager.Post(firstNotification);
271 /// Notification secondNotification = new Notification
274 /// Content = "content",
275 /// Icon = "absolute icon path",
276 /// Tag = "second notification"
278 /// NotificationManager.Post(secondNotification);
279 /// NotificationManager.DeleteAll();
282 /// <privilege>http://tizen.org/privilege/notification</privilege>
283 /// <since_tizen> 3 </since_tizen>
284 public static void DeleteAll()
286 NotificationError ret;
288 ret = Interop.Notification.DeleteAll((int)NotificationType.Basic);
289 if (ret != NotificationError.None)
291 throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Noti type");
294 ret = Interop.Notification.DeleteAll((int)NotificationType.Ongoing);
295 if (ret != NotificationError.None)
297 throw NotificationErrorFactory.GetException(ret, "delete all notifications failed of Ongoing type");
302 /// Searches for a posted notification which has the specified tag and has not been deleted yet.
305 /// Load method should be called only for notifications, which have been posted using the NotificationManager.Post method.
306 /// If two or more notifications share the same tag, the notification posted most recently is returned.
308 /// <param name="tag">Tag used to query.</param>
309 /// <returns>Notification Object with specified tag.</returns>
310 /// <exception cref="ArgumentException">Throwing the same exception when argument is invalid and when the tag does not exist is misleading.</exception>
311 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
312 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
315 /// Notification notification = new Notification
318 /// Content = "content",
319 /// Icon = "absolute icon path",
320 /// Tag = "first notification"
322 /// NotificationManager.Post(notification);
326 /// Notification loadNotification = NotificationManager.Load("first notification");
329 /// <privilege>http://tizen.org/privilege/notification</privilege>
330 /// <since_tizen> 3 </since_tizen>
331 public static Notification Load(string tag)
333 IntPtr ptr = IntPtr.Zero;
335 if (string.IsNullOrEmpty(tag))
337 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
340 ptr = Interop.Notification.Load(tag);
342 if (ptr == IntPtr.Zero)
344 NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
345 Log.Error(Notification.LogTag, "unable to load Notification : " + ret.ToString());
346 if (ret == NotificationError.DbError)
348 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the tag does not exist");
352 throw NotificationErrorFactory.GetException(ret, "unable to load Notification");
356 Notification notification = new Notification
358 Handle = new NotificationSafeHandle(ptr, true)
365 /// Saves a notification template to the notification database.
367 /// <param name="notification">Notification to save as template.</param>
368 /// <param name="name">Template name.</param>
369 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
370 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
371 /// <exception cref="InvalidOperationException">Thrown when it can't be saved as a template.</exception>
374 /// Notification notification = new Notification
377 /// Content = "content",
378 /// Icon = "absolute icon path",
379 /// Tag = "first notification"
382 /// Notification.Accessory accessory = new Notification.Accessory
384 /// LedOption = AccessoryOption.On,
385 /// VibrationOption = AccessoryOption.Custom,
386 /// VibrationPath = "vibration absolute path"
388 /// notification.setAccessory(accessory);
392 /// NotificationManager.Post(notification);
394 /// Notification.LockStyle style = new Notification.LockStyle
396 /// IconPath = "icon path",
397 /// ThumbnailPath = "Thumbnail path"
399 /// notification.AddStyle(style);
400 /// NotificationManager.SaveTemplate(notification, "firstTemplate");
403 /// <privilege>http://tizen.org/privilege/notification</privilege>
404 /// <since_tizen> 3 </since_tizen>
405 public static void SaveTemplate(Notification notification, string name)
407 if (notification == null || string.IsNullOrEmpty(name))
409 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to save template");
414 NotificationError ret = Interop.Notification.SaveTemplate(notification.Handle, name);
415 if (ret != NotificationError.None)
417 throw NotificationErrorFactory.GetException(ret, "save as template failed");
422 /// Loads a notification template from the notification database.
424 /// <param name="name">Template name.</param>
425 /// <returns>Notification Object with inputted template name.</returns>
426 /// <exception cref="ArgumentException">Throwing the same exception when argument is invalid and when the template does not exist is misleading.</exception>
427 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
428 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
431 /// Notification notification = new Notification
434 /// Content = "content",
435 /// Icon = "absolute icon path",
436 /// Tag = "first notification"
439 /// Notification.Accessory accessory = new Notification.Accessory
441 /// LedOption = AccessoryOption.On,
442 /// VibrationOption = AccessoryOption.Custom,
443 /// VibrationPath = "vibration absolute path"
445 /// notification.setAccessory(accessory);
449 /// NotificationManager.Post(notification);
451 /// Notification.LockStyle style = new Notification.LockStyle
453 /// IconPath = "icon path",
454 /// ThumbnailPath = "Thumbnail path"
456 /// notification.AddStyle(style);
457 /// NotificationManager.SaveTemplate(notification, "firstTemplate");
458 /// Notification notificationTemplate = NotificationManager.LoadTemplate("firstTemplate");
461 /// <privilege>http://tizen.org/privilege/notification</privilege>
462 /// <since_tizen> 3 </since_tizen>
463 public static Notification LoadTemplate(string name)
465 IntPtr handle = IntPtr.Zero;
467 if (string.IsNullOrEmpty(name))
469 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to load template");
472 handle = Interop.Notification.LoadTemplate(name);
473 if (handle == IntPtr.Zero)
475 NotificationError ret = (NotificationError)Tizen.Internals.Errors.ErrorFacts.GetLastResult();
476 if (ret == NotificationError.DbError)
478 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "the name does not exist");
482 throw NotificationErrorFactory.GetException(ret, "unable to create Notification from template");
486 Notification notification = new Notification
488 Handle = new NotificationSafeHandle(handle, true)
495 /// Gets notification block state.
498 /// The user can set the notification block state in settings.
499 /// The block state indicates whether or not notifications can be posted.
500 /// Additionally, only notifications to the notification panel are allowed in "Do not disturb mode".
501 /// Sound, vibrate, and active notifications are blocked.
503 /// <returns>NotificationBlockState is a state if notification is posted.</returns>
504 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission is denied.</exception>
505 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
506 /// <privilege>http://tizen.org/privilege/notification</privilege>
507 /// <since_tizen> 3 </since_tizen>
508 public static NotificationBlockState GetBlockState()
510 NotificationBlockState state;
511 NotificationError ret;
513 ret = Interop.Notification.GetBlockState(out state);
514 if (ret != NotificationError.None)
516 throw NotificationErrorFactory.GetException(ret, "GetBlockState failed");
519 Log.Info(Notification.LogTag, "Current block state is " + state.ToString());
524 /// Make a NotificationSafeHandle from Notification.
526 /// <param name="notification">The Notification class.</param>
527 /// <returns>The NotificationSafeHandle class.</returns>
528 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
529 /// <since_tizen> 3 </since_tizen>
530 [EditorBrowsable(EditorBrowsableState.Never)]
531 public static NotificationSafeHandle MakeNotificationSafeHandle(Notification notification)
533 if (notification == null)
535 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid notification object");
540 return notification.Handle;
544 /// Make a Notification from NotificationSafeHandle.
546 /// <param name="handle">The NotificationSafeHandle class.</param>
547 /// <returns>The Notification class.</returns>
548 /// <exception cref="ArgumentException">Thrown when an argument is invalid.</exception>
549 /// <since_tizen> 3 </since_tizen>
550 [EditorBrowsable(EditorBrowsableState.Never)]
551 public static Notification MakeNotification(NotificationSafeHandle handle)
553 if (handle == null || handle.IsInvalid == true)
555 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "handle is invalid or null");
558 Notification notification = new Notification { Handle = handle }.Build();