diff --git a/Plugin.Notifications/Notification.cs b/Plugin.Notifications/Notification.cs index c178350..349d7c4 100644 --- a/Plugin.Notifications/Notification.cs +++ b/Plugin.Notifications/Notification.cs @@ -7,14 +7,18 @@ namespace Plugin.Notifications public class Notification { + public const string PlatformDefault = "..PLATFORM_DEFAULT.."; + public static string DefaultTitle { get; set; } public static string DefaultSound { get; set; } + public static string DefaultIcon { get; set; } public int? Id { get; set; } public string Title { get; set; } = DefaultTitle; public string Message { get; set; } public string Sound { get; set; } = DefaultSound; + public string Icon { get; set; } = DefaultIcon; public IDictionary Metadata { get; set; } = new Dictionary(); /// diff --git a/Plugin.Notifications/Platforms/Android/NotificationsImpl.cs b/Plugin.Notifications/Platforms/Android/NotificationsImpl.cs index ea4ec8e..01dc291 100644 --- a/Plugin.Notifications/Platforms/Android/NotificationsImpl.cs +++ b/Plugin.Notifications/Platforms/Android/NotificationsImpl.cs @@ -75,11 +75,14 @@ public override Task Send(Notification notification) .AddNextIntent(launchIntent) .GetPendingIntent(notification.Id.Value, (int)(PendingIntentFlags.OneShot | PendingIntentFlags.CancelCurrent)); + int iconResId = notification.Icon != null ? AndroidConfig.GetResourceIdByName(notification.Icon) + : AndroidConfig.AppIconResourceId; + var builder = new NotificationCompat.Builder(Application.Context) .SetAutoCancel(true) .SetContentTitle(notification.Title) .SetContentText(notification.Message) - .SetSmallIcon(AndroidConfig.AppIconResourceId) + .SetSmallIcon(iconResId) .SetContentIntent(pendingIntent); if (notification.Vibrate) @@ -87,10 +90,23 @@ public override Task Send(Notification notification) if (notification.Sound != null) { - if (!notification.Sound.Contains("://")) + Android.Net.Uri uri; + + if (notification.Sound == Notification.PlatformDefault) + { + // Fallback to the system default notification sound + uri = Android.Media.RingtoneManager.GetDefaultUri(Android.Media.RingtoneType.Notification); + } + else if (!notification.Sound.Contains("://")) + { notification.Sound = $"{ContentResolver.SchemeAndroidResource}://{Application.Context.PackageName}/raw/{notification.Sound}"; + uri = Android.Net.Uri.Parse(notification.Sound); + } + else + { + uri = Android.Net.Uri.Parse(notification.Sound); + } - var uri = Android.Net.Uri.Parse(notification.Sound); builder.SetSound(uri); } var not = builder.Build(); diff --git a/Plugin.Notifications/Platforms/Uwp/NotificationsImpl.cs b/Plugin.Notifications/Platforms/Uwp/NotificationsImpl.cs index 5dc4395..d632230 100644 --- a/Plugin.Notifications/Platforms/Uwp/NotificationsImpl.cs +++ b/Plugin.Notifications/Platforms/Uwp/NotificationsImpl.cs @@ -81,17 +81,27 @@ public override Task Send(Notification notification) } } }; - if (!String.IsNullOrWhiteSpace(notification.Sound) && this.IsAudioSupported) - { - if (!notification.Sound.StartsWith("ms-appx:")) - notification.Sound = $"ms-appx:///Assets/Audio/{notification.Sound}.m4a"; - toastContent.Audio = new ToastAudio + if (IsAudioSupported) + { + if (notification.Sound == Notification.PlatformDefault) { - Src = new Uri(notification.Sound) - }; - } + toastContent.Audio = new ToastAudio + { + Src = new Uri("ms-winsoundevent:Notification.Looping.Alarm") + }; + } + else if (!String.IsNullOrWhiteSpace(notification.Sound)) + { + if (!notification.Sound.StartsWith("ms-appx:")) + notification.Sound = $"ms-appx:///Assets/Audio/{notification.Sound}.m4a"; + toastContent.Audio = new ToastAudio + { + Src = new Uri(notification.Sound) + }; + } + } if (notification.ScheduledDate == null) { diff --git a/Plugin.Notifications/Platforms/iOS/UILocalNotificationsImpl.cs b/Plugin.Notifications/Platforms/iOS/UILocalNotificationsImpl.cs index dedaea6..ae7167d 100644 --- a/Plugin.Notifications/Platforms/iOS/UILocalNotificationsImpl.cs +++ b/Plugin.Notifications/Platforms/iOS/UILocalNotificationsImpl.cs @@ -58,13 +58,18 @@ public override Task Send(Notification notification) if (notification.Id == null) notification.GeneratedNotificationId(); + if (notification.Sound == Notification.PlatformDefault) + notification.Sound = UILocalNotification.DefaultSoundName; + var not = new UILocalNotification { AlertTitle = notification.Title, AlertBody = notification.Message, - SoundName = notification.Sound, - UserInfo = notification.MetadataToNsDictionary() + AlertLaunchImage = notification.Icon, + UserInfo = notification.MetadataToNsDictionary(), + SoundName = notification.Sound }; + if (notification.ScheduledDate != null) not.FireDate = notification.ScheduledDate.Value.ToNSDate(); diff --git a/Plugin.Notifications/Platforms/iOS/UNNotificationsImpl.cs b/Plugin.Notifications/Platforms/iOS/UNNotificationsImpl.cs index a6380ee..579e309 100644 --- a/Plugin.Notifications/Platforms/iOS/UNNotificationsImpl.cs +++ b/Plugin.Notifications/Platforms/iOS/UNNotificationsImpl.cs @@ -48,10 +48,18 @@ public override Task Send(Notification notification) => this.Invoke(async () => { Title = notification.Title, Body = notification.Message, - UserInfo = notification.MetadataToNsDictionary() + UserInfo = notification.MetadataToNsDictionary(), + LaunchImageName = notification.Icon }; - if (!String.IsNullOrWhiteSpace(notification.Sound)) + + if (notification.Sound == Notification.PlatformDefault) + { + content.Sound = UNNotificationSound.Default; + } + else if (!String.IsNullOrWhiteSpace(notification.Sound)) + { content.Sound = UNNotificationSound.GetSound(notification.Sound); + } var dt = notification.ScheduledDate ?? DateTime.Now; var request = UNNotificationRequest.FromIdentifier( diff --git a/Plugin.Notifications/Platforms/macOS/NotificationsImpl.cs b/Plugin.Notifications/Platforms/macOS/NotificationsImpl.cs index 9690f1f..4497d7f 100644 --- a/Plugin.Notifications/Platforms/macOS/NotificationsImpl.cs +++ b/Plugin.Notifications/Platforms/macOS/NotificationsImpl.cs @@ -43,6 +43,11 @@ public override Task Send(Notification notification) => this.Invoke(() => if (notification.Id == null) notification.GeneratedNotificationId(); + if (notification.Sound == Notification.PlatformDefault) + { + notification.Sound = NSUserNotification.NSUserNotificationDefaultSoundName; + } + var native = new NSUserNotification { Identifier = notification.Id.Value.ToString(),