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.Collections.Generic;
21 using System.ComponentModel;
24 /// This class contains common properties and methods of notifications.
27 /// A notification is a message that is displayed on the notification area.
28 /// It is created to notify information to the user through the application.
29 /// This class helps you to provide method and property for creating notification object.
31 public sealed partial class Notification : IDisposable
33 internal static readonly string LogTag = "Tizen.Applications.Notification";
35 private NotificationSafeHandle safeHandle;
36 private bool disposed = false;
38 private IDictionary<string, StyleBase> styleDictionary;
39 private IDictionary<string, Bundle> extraDataDictionary;
40 private int count = 0;
43 /// Initializes a new instance of the <see cref="Notification"/> class.
45 /// <since_tizen> 3 </since_tizen>
48 styleDictionary = new Dictionary<string, StyleBase>();
49 extraDataDictionary = new Dictionary<string, Bundle>();
53 /// Gets or sets the tag of notification.
55 /// <since_tizen> 3 </since_tizen>
56 public string Tag { get; set; } = string.Empty;
59 /// Gets or sets the title of notification.
61 /// <since_tizen> 3 </since_tizen>
62 public string Title { get; set; } = string.Empty;
65 /// Gets or sets the icon of notification.
66 /// You should set an absolute path for an image file.
68 /// <since_tizen> 3 </since_tizen>
69 public string Icon { get; set; } = string.Empty;
72 /// Gets or sets the sub icon of notification.
73 /// This SubIcon is displayed in Icon you set.
74 /// You should set an absolute path for an image file.
76 /// <since_tizen> 3 </since_tizen>
77 public string SubIcon { get; set; } = string.Empty;
80 /// Gets or sets the content of notification.
82 /// <since_tizen> 3 </since_tizen>
83 public string Content { get; set; } = string.Empty;
86 /// Gets or sets a value indicating whether TimeStamp of the notification is Visible or not.
89 /// <since_tizen> 3 </since_tizen>
90 public bool IsTimeStampVisible { get; set; } = true;
93 /// Gets or sets the TimeStamp of notification.
96 /// If you don't set TimeStamp, it will set the value when the notification is posted.
97 /// TimeStamp requires NotificationManager.Post() to be called.
98 /// If you set IsVisibleTimeStamp property to false, TimeStamp is not visible in notification.
100 /// <since_tizen> 3 </since_tizen>
101 public DateTime TimeStamp { get; set; }
104 /// Gets or sets action, which is invoked when the notification is clicked.
107 /// If you set it to null, the already set AppControl will be removed and nothing will happen when you click on notification.
109 /// <seealso cref="Tizen.Applications.AppControl"></seealso>
110 /// <since_tizen> 3 </since_tizen>
111 public AppControl Action { get; set; }
114 /// Gets or sets count, which is displayed at the right side of the notification.
117 /// You must set only positive number.
118 /// If you set count to negative number, this property throws exception.
120 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
121 /// <since_tizen> 3 </since_tizen>
133 Log.Error(LogTag, "Count value is negative");
134 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The Count must be a positive integer.");
142 /// Gets or sets a value indicating whether the notification is Onging or not.
143 /// Default value is false.
145 /// <since_tizen> 3 </since_tizen>
146 [EditorBrowsable(EditorBrowsableState.Never)]
147 public bool IsOngoing { get; set; } = false;
150 /// Gets or sets property.
152 /// <seealso cref="Tizen.Applications.Notifications.NotificationProperty"></seealso>
153 /// <since_tizen> 3 </since_tizen>
154 public NotificationProperty Property { get; set; } = NotificationProperty.None;
157 /// Gets or sets <see cref="Notification.ProgressType"/> object for display at notification.
159 /// <seealso cref="Tizen.Applications.Notifications.Notification.ProgressType"></seealso>
160 /// <since_tizen> 3 </since_tizen>
161 public ProgressType Progress { get; set; }
164 /// Gets or sets <see cref="Notification.AccessorySet"/> which is included vibration, LED and sound option to be applied at notification.
167 /// If you set it to null, the already set AccessorySet will be initialized.
171 /// Notification notification = new Notification
173 /// Title = "Notification",
174 /// Content = "Hello Tizen",
175 /// Icon = "Icon path",
179 /// Notification.AccessorySet accessory = new Notification.AccessorySet
181 /// SoundOption = AccessoryOption.Custom,
182 /// SoundPath = "Sound File Path",
183 /// IsVibration = true,
184 /// LedOption = AccessoryOption.Custom,
187 /// LedColor = Color.Lime
190 /// notification.Accessory = accessory;
192 /// NotificationManager.Post(notification);
195 /// <since_tizen> 3 </since_tizen>
196 public AccessorySet Accessory { get; set; }
199 /// Gets or sets a value indicating whether notification is displayed on the default viewer.
200 /// If you set false and add style, you can see only style notification.
202 /// <since_tizen> 4 </since_tizen>
203 public bool IsVisible { get; set; } = true;
206 /// Gets or sets NotificationSafeHandle.
208 internal NotificationSafeHandle Handle
219 Log.Error(LogTag, "Invalid argument NotificationSafeHandle");
220 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid argument to set NotificationSafeHandle");
228 /// Gets or sets private ID.
230 internal int PrivID { get; set; } = -1;
233 /// Method for adding various styles to be applied to notification.
236 /// The user always see about valid notification style. If you add a style which is not supported in platform,
237 /// this method has no effect.
239 /// <param name="style">The style to be applied to notification.</param>
240 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
243 /// Notification notification = new Notification
245 /// Title = "Notification",
246 /// Content = "Hello Tizen",
247 /// Icon = "Icon path",
251 /// Notification.LockStyle lockStyle = new Notification.LockStyle
253 /// IconPath = "Icon path",
254 /// ThumbnailPath = "Thumbnail Path"
257 /// notification.AddStyle(lockStyle);
259 /// NotificationManager.Post(notification);
262 /// <since_tizen> 3 </since_tizen>
263 public void AddStyle(StyleBase style)
267 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
270 if (styleDictionary.ContainsKey(style.Key) == true)
272 Log.Info(LogTag, "The Style is existed, so extender data is replaced");
273 styleDictionary.Remove(style.Key);
274 styleDictionary.Add(style.Key, style);
278 styleDictionary.Add(style.Key, style);
283 /// Method to remove style you already added.
285 /// <typeparam name="T">Type of notification style to be queried.</typeparam>
286 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
287 /// <since_tizen> 3 </since_tizen>
288 public void RemoveStyle<T>() where T : Notification.StyleBase, new()
292 if (styleDictionary.ContainsKey(type.Key))
294 styleDictionary.Remove(type.Key);
298 Log.Error(LogTag, "Sytle Can't be removed, there is no style matched input key");
299 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
304 /// Method to get style you already added.
306 /// <typeparam name="T">Type of notification style to be queried.</typeparam>
308 /// The Notification.Style object associated with the given style.
310 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
311 /// <since_tizen> 3 </since_tizen>
312 public T GetStyle<T>() where T : Notification.StyleBase, new()
315 StyleBase style = null;
317 styleDictionary.TryGetValue(type.Key, out style);
321 Log.Error(LogTag, "Invalid Style");
322 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
331 /// Method to set extra data to add extra data.
334 /// The type of extra data is bundle.
336 /// <param name="key">The key of the extra data you want to add.</param>
337 /// <param name="value">The value you want to add.</param>
338 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
341 /// Notification notification = new Notification
343 /// Title = "Notification",
344 /// Content = "Hello Tizen",
345 /// Icon = "Icon path",
348 /// Bundle bundle = new Bundle();
349 /// bundle.AddItem("key", "value");
351 /// notification.SetExtraData("firstKey", bundle);
354 /// <since_tizen> 4 </since_tizen>
355 public void SetExtraData(string key, Bundle value)
357 if (value == null || value.SafeBundleHandle.IsInvalid || string.IsNullOrEmpty(key))
359 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
362 if (extraDataDictionary.ContainsKey(key) == true)
364 Log.Info(LogTag, "The key is existed, so extender data is replaced");
365 extraDataDictionary.Remove(key);
366 extraDataDictionary.Add(key, value);
370 extraDataDictionary.Add(key, value);
375 /// Method to remove extra you already added.
378 /// The type of extra data is bundle.
380 /// <param name="key">The key of the extra data to add.</param>
381 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
382 /// <since_tizen> 4 </since_tizen>
383 public void RemoveExtraData(string key)
385 if (string.IsNullOrEmpty(key))
387 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
390 if (extraDataDictionary.ContainsKey(key))
392 extraDataDictionary.Remove(key);
396 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
401 /// Method to get extra data you already set.
403 /// <param name="key">The key of the extra data to get.</param>
404 /// <returns>Bundle Object that include extra data</returns>
405 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
406 /// <since_tizen> 4 </since_tizen>
407 public Bundle GetExtraData(string key)
409 if (string.IsNullOrEmpty(key))
411 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered");
415 if (extraDataDictionary.TryGetValue(key, out bundle) == false)
417 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "invalid parameter entered : " + key);
424 /// Releases any unmanaged resources used by this object.
426 /// <since_tizen> 3 </since_tizen>
427 public void Dispose()
430 GC.SuppressFinalize(this);
433 internal void Dispose(bool disposing)
440 if (disposing && Handle != null && Handle.IsInvalid == false)
448 internal IDictionary<string, StyleBase> GetStyleDictionary()
450 return styleDictionary;
453 internal IDictionary<string, Bundle> GetextraDataDictionary()
455 return extraDataDictionary;
458 internal StyleBase GetStyle(string key)
460 if (string.IsNullOrEmpty(key))
462 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "Key is null or empty");
465 StyleBase style = null;
466 bool ret = styleDictionary.TryGetValue(key, out style);
469 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "The Style object matched input key is not existed");
477 NotificationBinder.BindObject(this);
479 foreach (string key in GetextraDataDictionary().Keys)
481 Log.Info(LogTag, "Start to bind Notification.ExtenderData to SafeHandle");
482 Interop.Notification.SetExtensionData(Handle, key, extraDataDictionary[key].SafeBundleHandle);
485 foreach (Notification.StyleBase style in styleDictionary.Values)
487 Log.Info(LogTag, "Start to bind Notification.Style to SafeHandle [" + style.Key + "]");
491 if (Accessory != null)
493 Log.Info(LogTag, "Start to bind Notification.AccessetSet to SafeHandle");
494 Accessory.Make(this);
497 if (Progress != null)
499 Log.Info(LogTag, "Start to bind Notification.Progress to SafeHandle");
504 internal Notification Build()
506 IntPtr extension = IntPtr.Zero;
507 IntPtr extensionBundlePtr = IntPtr.Zero;
509 NotificationBinder.BindSafeHandle(this);
511 Interop.Notification.GetExtensionBundle(Handle, out extension, out extensionBundlePtr);
513 if (extension != IntPtr.Zero)
515 Bundle bundle = new Bundle(new SafeBundleHandle(extension, false));
516 foreach (string key in bundle.Keys)
518 if (key.StartsWith("_NOTIFICATION_EXTENSION_EVENT_"))
521 SafeBundleHandle sbh;
522 Interop.Notification.GetExtensionData(Handle, key, out sbh);
523 extraDataDictionary.Add(key, new Bundle(sbh));
527 ProgressBinder.BindSafeHandle(this);
528 AccessorySetBinder.BindSafeHandle(this);
529 IndicatorBinder.BindSafeHandle(this);
530 ActiveBinder.BindSafeHandle(this);
531 LockBinder.BindSafehandle(this);