-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
239 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
Source/Plugin.LocalNotification/Platforms/Windows/LocalNotificationCenter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Toolkit.Uwp.Notifications; | ||
using Plugin.LocalNotification.EventArgs; | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
|
||
namespace Plugin.LocalNotification | ||
{ | ||
public partial class LocalNotificationCenter | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="permission"></param> | ||
/// <returns></returns> | ||
public static async Task<bool> RequestNotificationPermissionAsync(NotificationPermission permission = null) | ||
{ | ||
return await Task.FromResult(true); | ||
} | ||
|
||
/// <summary> | ||
/// Notify Local Notification Tapped. | ||
/// </summary> | ||
/// <param name="arguments"></param> | ||
public static void NotifyNotificationTapped(string arguments) | ||
{ | ||
try | ||
{ | ||
var args = ToastArguments.Parse(arguments); | ||
|
||
var actionId = args.GetInt(ReturnRequestActionId); | ||
if (actionId == -1000) | ||
{ | ||
return; | ||
} | ||
if (args.TryGetValue(ReturnRequest, out var requestSerialize)) | ||
{ | ||
return; | ||
} | ||
var request = GetRequest(requestSerialize); | ||
|
||
var actionArgs = new NotificationActionEventArgs | ||
{ | ||
ActionId = actionId, | ||
Request = request | ||
}; | ||
Current.OnNotificationActionTapped(actionArgs); | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
Log(ex); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="message"></param> | ||
/// <param name="callerName"></param> | ||
internal static void Log(string message, [CallerMemberName] string callerName = "") | ||
{ | ||
var logMessage = $"{callerName}: {message}"; | ||
Logger?.Log(LogLevel, logMessage); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="ex"></param> | ||
/// <param name="message"></param> | ||
/// <param name="callerName"></param> | ||
internal static void Log(Exception ex, string message = null, [CallerMemberName] string callerName = "") | ||
{ | ||
var logMessage = $"{callerName}: {message}"; | ||
Logger?.LogError(ex, logMessage); | ||
} | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
Source/Plugin.LocalNotification/Platforms/Windows/NotificationServiceImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using Microsoft.Toolkit.Uwp.Notifications; | ||
using Plugin.LocalNotification.EventArgs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Windows.UI.Notifications; | ||
|
||
namespace Plugin.LocalNotification.Platforms | ||
{ | ||
internal class NotificationServiceImpl : INotificationService | ||
{ | ||
private readonly IList<NotificationCategory> _categoryList = new List<NotificationCategory>(); | ||
|
||
public Func<NotificationRequest, Task<NotificationEventReceivingArgs>> NotificationReceiving { get; set; } | ||
|
||
public event NotificationActionTappedEventHandler NotificationActionTapped; | ||
public event NotificationReceivedEventHandler NotificationReceived; | ||
public event NotificationDisabledEventHandler NotificationsDisabled; | ||
|
||
public Task<bool> AreNotificationsEnabled() => throw new NotImplementedException(); | ||
|
||
public bool Cancel(params int[] notificationIdList) => throw new NotImplementedException(); | ||
|
||
public bool CancelAll() => throw new NotImplementedException(); | ||
|
||
public bool Clear(params int[] notificationIdList) => throw new NotImplementedException(); | ||
|
||
public bool ClearAll() => throw new NotImplementedException(); | ||
|
||
public Task<IList<NotificationRequest>> GetDeliveredNotificationList() => throw new NotImplementedException(); | ||
|
||
public Task<IList<NotificationRequest>> GetPendingNotificationList() => throw new NotImplementedException(); | ||
|
||
public void OnNotificationActionTapped(NotificationActionEventArgs e) | ||
{ | ||
NotificationActionTapped?.Invoke(e); | ||
} | ||
|
||
public void OnNotificationReceived(NotificationEventArgs e) | ||
{ | ||
NotificationReceived?.Invoke(e); | ||
} | ||
|
||
public void OnNotificationsDisabled() | ||
{ | ||
NotificationsDisabled?.Invoke(); | ||
} | ||
|
||
public void RegisterCategoryList(HashSet<NotificationCategory> categoryList) | ||
{ | ||
if (categoryList is null || categoryList.Any() == false) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var category in categoryList.Where(category => | ||
category.CategoryType != NotificationCategoryType.None)) | ||
{ | ||
_categoryList.Add(category); | ||
} | ||
} | ||
|
||
public Task<bool> RequestNotificationPermission(NotificationPermission permission = null) | ||
{ | ||
return LocalNotificationCenter.RequestNotificationPermissionAsync(permission); | ||
} | ||
|
||
public Task<bool> Show(NotificationRequest request) | ||
{ | ||
var serializedRequest = LocalNotificationCenter.GetRequestSerialize(request); | ||
|
||
var builder = new ToastContentBuilder() | ||
.AddArgument(LocalNotificationCenter.ReturnRequestActionId, NotificationActionEventArgs.TapActionId) | ||
.AddArgument(LocalNotificationCenter.ReturnRequest, serializedRequest) | ||
.AddHeader(nameof(request.NotificationId), request.Group, string.Empty) | ||
.AddText(request.Title) | ||
.AddText(request.Subtitle) | ||
.AddText(request.Description); | ||
|
||
if (request.Schedule != null && request.Schedule.NotifyTime != null) | ||
{ | ||
builder.Schedule(new DateTimeOffset(request.Schedule.NotifyTime.Value)); | ||
}; | ||
|
||
if (_categoryList.Any()) | ||
{ | ||
var categoryByType = _categoryList.FirstOrDefault(c => c.CategoryType == request.CategoryType); | ||
if (categoryByType != null) | ||
{ | ||
foreach (var notificationAction in categoryByType.ActionList) | ||
{ | ||
builder.AddButton(new ToastButton() | ||
.SetContent(notificationAction.Title) | ||
.AddArgument(LocalNotificationCenter.ReturnRequestActionId, notificationAction.ActionId) | ||
.AddArgument(LocalNotificationCenter.ReturnRequest, serializedRequest)); | ||
} | ||
} | ||
} | ||
|
||
builder.Show(toast => | ||
{ | ||
toast.Activated += (sender, args) => | ||
{ | ||
var toastArgs = args as ToastActivatedEventArgs; | ||
LocalNotificationCenter.NotifyNotificationTapped(toastArgs.Arguments); | ||
}; | ||
toast.Dismissed += (sender, args) => | ||
{ | ||
var arguments = $"{LocalNotificationCenter.ReturnRequestActionId}={NotificationActionEventArgs.DismissedActionId};"; | ||
arguments += $"{LocalNotificationCenter.ReturnRequest}={serializedRequest}"; | ||
LocalNotificationCenter.NotifyNotificationTapped(arguments); | ||
}; | ||
}); | ||
|
||
return Task.FromResult(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters