mirror of
https://github.com/ElectronNET/Electron.NET.git
synced 2026-07-11 19:27:52 +00:00
implement full Notification-API
This commit is contained in:
15
ElectronNET.API/Entities/NotificationAction.cs
Normal file
15
ElectronNET.API/Entities/NotificationAction.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
public class NotificationAction
|
||||
{
|
||||
/// <summary>
|
||||
/// The label for the given action.
|
||||
/// </summary>
|
||||
public string Text { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The type of action, can be button.
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
namespace ElectronNET.API.Entities
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
|
||||
namespace ElectronNET.API.Entities
|
||||
{
|
||||
public class NotificationOptions
|
||||
{
|
||||
@@ -44,6 +47,63 @@
|
||||
/// </summary>
|
||||
public string Sound { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Actions to add to the notification. Please read the available actions and
|
||||
/// limitations in the NotificationAction documentation
|
||||
/// </summary>
|
||||
public NotificationAction Actions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Emitted when the notification is shown to the user,
|
||||
/// note this could be fired multiple times as a notification
|
||||
/// can be shown multiple times through the Show() method.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public Action OnShow { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
internal string ShowID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Emitted when the notification is clicked by the user.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public Action OnClick { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
internal string ClickID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Emitted when the notification is closed by manual intervention from the user.
|
||||
///
|
||||
/// This event is not guarunteed to be emitted in all cases where the notification is closed.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public Action OnClose { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
internal string CloseID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// macOS only: Emitted when the user clicks the “Reply” button on a notification with hasReply: true.
|
||||
///
|
||||
/// The string the user entered into the inline reply field
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public Action<string> OnReply { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
internal string ReplyID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// macOS only - The index of the action that was activated
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public Action<string> OnAction { get; set; }
|
||||
|
||||
[JsonProperty]
|
||||
internal string ActionID { get; set; }
|
||||
|
||||
public NotificationOptions(string title, string body)
|
||||
{
|
||||
Title = title;
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ElectronNET.API
|
||||
{
|
||||
@@ -24,11 +28,105 @@ namespace ElectronNET.API
|
||||
}
|
||||
}
|
||||
|
||||
private static List<NotificationOptions> _notificationOptions = new List<NotificationOptions>();
|
||||
|
||||
/// <summary>
|
||||
/// Create OS desktop notifications
|
||||
/// </summary>
|
||||
/// <param name="notificationOptions"></param>
|
||||
public void Show(NotificationOptions notificationOptions)
|
||||
{
|
||||
GenerateIDsForDefinedActions(notificationOptions);
|
||||
|
||||
BridgeConnector.Socket.Emit("createNotification", JObject.FromObject(notificationOptions, _jsonSerializer));
|
||||
}
|
||||
|
||||
private static void GenerateIDsForDefinedActions(NotificationOptions notificationOptions)
|
||||
{
|
||||
bool isActionDefined = false;
|
||||
|
||||
if (notificationOptions.OnShow != null)
|
||||
{
|
||||
notificationOptions.ShowID = Guid.NewGuid().ToString();
|
||||
isActionDefined = true;
|
||||
|
||||
BridgeConnector.Socket.Off("NotificationEventShow");
|
||||
BridgeConnector.Socket.On("NotificationEventShow", (id) => {
|
||||
_notificationOptions.Single(x => x.ShowID == id.ToString()).OnShow();
|
||||
});
|
||||
}
|
||||
|
||||
if (notificationOptions.OnClick != null)
|
||||
{
|
||||
notificationOptions.ClickID = Guid.NewGuid().ToString();
|
||||
isActionDefined = true;
|
||||
|
||||
BridgeConnector.Socket.Off("NotificationEventClick");
|
||||
BridgeConnector.Socket.On("NotificationEventClick", (id) => {
|
||||
_notificationOptions.Single(x => x.ClickID == id.ToString()).OnClick();
|
||||
});
|
||||
}
|
||||
|
||||
if (notificationOptions.OnClose != null)
|
||||
{
|
||||
notificationOptions.CloseID = Guid.NewGuid().ToString();
|
||||
isActionDefined = true;
|
||||
|
||||
BridgeConnector.Socket.Off("NotificationEventClose");
|
||||
BridgeConnector.Socket.On("NotificationEventClose", (id) => {
|
||||
_notificationOptions.Single(x => x.CloseID == id.ToString()).OnClose();
|
||||
});
|
||||
}
|
||||
|
||||
if (notificationOptions.OnReply != null)
|
||||
{
|
||||
notificationOptions.ReplyID = Guid.NewGuid().ToString();
|
||||
isActionDefined = true;
|
||||
|
||||
BridgeConnector.Socket.Off("NotificationEventReply");
|
||||
BridgeConnector.Socket.On("NotificationEventReply", (args) => {
|
||||
var arguments = ((JArray)args).ToObject<string[]>();
|
||||
_notificationOptions.Single(x => x.ReplyID == arguments[0].ToString()).OnReply(arguments[1].ToString());
|
||||
});
|
||||
}
|
||||
|
||||
if (notificationOptions.OnAction != null)
|
||||
{
|
||||
notificationOptions.ActionID = Guid.NewGuid().ToString();
|
||||
isActionDefined = true;
|
||||
|
||||
BridgeConnector.Socket.Off("NotificationEventAction");
|
||||
BridgeConnector.Socket.On("NotificationEventAction", (args) => {
|
||||
var arguments = ((JArray)args).ToObject<string[]>();
|
||||
_notificationOptions.Single(x => x.ReplyID == arguments[0].ToString()).OnAction(arguments[1].ToString());
|
||||
});
|
||||
}
|
||||
|
||||
if (isActionDefined)
|
||||
{
|
||||
_notificationOptions.Add(notificationOptions);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not desktop notifications are supported on the current system.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<bool> IsSupportedAsync()
|
||||
{
|
||||
var taskCompletionSource = new TaskCompletionSource<bool>();
|
||||
|
||||
BridgeConnector.Socket.On("notificationIsSupportedComplete", (isSupported) =>
|
||||
{
|
||||
BridgeConnector.Socket.Off("notificationIsSupportedComplete");
|
||||
taskCompletionSource.SetResult((bool)isSupported);
|
||||
});
|
||||
|
||||
BridgeConnector.Socket.Emit("notificationIsSupported");
|
||||
|
||||
return taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
private JsonSerializer _jsonSerializer = new JsonSerializer()
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
|
||||
Reference in New Issue
Block a user