Skip to content

Commit

Permalink
Adding windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
thudugala committed Aug 12, 2023
1 parent 7a37253 commit 1ce151f
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Source/Plugin.LocalNotification/INotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public interface INotificationService
/// <param name="e"></param>
void OnNotificationReceived(NotificationEventArgs e);

/// <summary>
/// Internal use Only
/// </summary>
void OnNotificationsDisabled();

/// <summary>
/// Get pending notifications
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Source/Plugin.LocalNotification/LocalNotificationCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;
#if ANDROID || IOS || MONOANDROID || XAMARINIOS
#if ANDROID || IOS || MONOANDROID || XAMARINIOS || WINDOWS
using Plugin.LocalNotification.Platforms;
#endif

Expand All @@ -20,7 +20,7 @@ public partial class LocalNotificationCenter

private static INotificationService CreateNotificationService()
{
#if ANDROID || IOS || MONOANDROID || XAMARINIOS
#if ANDROID || IOS || MONOANDROID || XAMARINIOS || WINDOWS
return new NotificationServiceImpl();
#else
return null;
Expand Down
21 changes: 20 additions & 1 deletion Source/Plugin.LocalNotification/LocalNotificationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using Microsoft.Maui.LifecycleEvents;
using System;
using System.Linq;
#if WINDOWS
using Microsoft.Toolkit.Uwp.Notifications;
#endif
#endif

namespace Plugin.LocalNotification
Expand Down Expand Up @@ -72,11 +75,27 @@ public static MauiAppBuilder UseLocalNotification(this MauiAppBuilder builder, A
LocalNotificationCenter.ResetApplicationIconBadgeNumber(application);
});
});
#elif WINDOWS
life.AddWindows(windows =>
{
windows.OnActivated((window, args) =>
{
ToastNotificationManagerCompat.OnActivated += (notificationArgs) =>
{
// this will run everytime ToastNotification.Activated is called,
// regardless of what toast is clicked and what element is clicked on.
// Works for all types of ToastActivationType so long as the Windows app manifest
// has been updated to support ToastNotifications.
LocalNotificationCenter.NotifyNotificationTapped(notificationArgs.Argument);
};
});
});
#endif
});

return builder;
}
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public void OnNotificationReceived(NotificationEventArgs e)
NotificationReceived?.Invoke(e);
}

/// <summary>
///
/// </summary>
/// <inheritdoc />
public void OnNotificationsDisabled()
{
NotificationsDisabled?.Invoke();
Expand Down
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);
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public void OnNotificationActionTapped(NotificationActionEventArgs e)
NotificationActionTapped?.Invoke(e);
}

/// <summary>
///
/// </summary>
/// <inheritdoc />
public void OnNotificationsDisabled()
{
NotificationsDisabled?.Invoke();
Expand Down
10 changes: 9 additions & 1 deletion Source/Plugin.LocalNotification/Plugin.LocalNotification.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;MonoAndroid12.0;Xamarin.iOS10;net6.0;net6.0-android;net6.0-ios;net7.0;net7.0-android;net7.0-ios</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0;net7.0-windows10.0.19041.0</TargetFrameworks>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<DefaultLanguage>en-US</DefaultLanguage>
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
Expand Down Expand Up @@ -41,6 +42,8 @@
<PropertyGroup>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-ios'))">10.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$(TargetFramework.Contains('-android'))">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.StartsWith('net6')) Or $(TargetFramework.StartsWith('net7'))">
Expand Down Expand Up @@ -69,7 +72,7 @@

<ItemGroup Condition="$(TargetFramework.StartsWith('MonoAndroid'))">
<Compile Include="**/Platforms/Android/**/*.cs" />
<PackageReference Include="Xamarin.Build.Download" Version="0.11.4" PrivateAssets="All"/>
<PackageReference Include="Xamarin.Build.Download" Version="0.11.4" PrivateAssets="All" />
<PackageReference Include="Xamarin.AndroidX.Core" Version="1.9.0.1" />
<PackageReference Include="Xamarin.GooglePlayServices.Location" Version="120.0.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand All @@ -91,6 +94,11 @@
<Compile Include="**/Platforms/iOS/**/*.cs" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.Contains('-windows'))">
<Compile Include="**/Platforms/Windows/**/*.cs" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down

0 comments on commit 1ce151f

Please sign in to comment.