Skip to content

Commit

Permalink
WIP default sound and icon
Browse files Browse the repository at this point in the history
  • Loading branch information
red-rj committed Nov 24, 2018
1 parent 85fd0fb commit 6c3f805
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Plugin.Notifications/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ namespace Plugin.Notifications

public class Notification
{
// null = sound, icon
// PlatformDefault = use platform option
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 Title { get; set; }
public string Message { get; set; }
public string Sound { get; set; } = DefaultSound;
public string Icon { get; set; } = DefaultIcon;
Expand Down
4 changes: 3 additions & 1 deletion Plugin.Notifications/Platforms/Android/AndroidConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Plugin.Notifications
{
public static class AndroidConfig
{
public static int AppIconResourceId { get; set; } = GetResourceIdByName("icon");
public static int DefaultIcon { get; set; } = GetResourceIdByName("icon");
public static Android.Net.Uri DefaultSound { get; set; } = Android.Media.RingtoneManager.GetDefaultUri(Android.Media.RingtoneType.Notification);

public static INotificationRepository Repository { get; set; } = new SqliteNotificationRepository();
public static ActivityFlags LaunchActivityFlags { get; set; } = ActivityFlags.NewTask | ActivityFlags.ClearTask;

Expand Down
24 changes: 20 additions & 4 deletions Plugin.Notifications/Platforms/Android/NotificationsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,30 @@ public override Task Send(Notification notification)
.GetPendingIntent(notification.Id.Value, (int)(PendingIntentFlags.OneShot | PendingIntentFlags.CancelCurrent));

int iconResId = notification.Icon != null ? AndroidConfig.GetResourceIdByName(notification.Icon)
: AndroidConfig.AppIconResourceId;
: AndroidConfig.DefaultIcon;

var builder = new NotificationCompat.Builder(Application.Context)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetContentTitle(notification.Title)
.SetContentText(notification.Message)
.SetSmallIcon(iconResId)
.SetContentIntent(pendingIntent);
;

if (notification.Icon != null)
{
int iconId;

if (notification.Icon == Notification.PlatformDefault)
{
iconId = AndroidConfig.DefaultIcon;
}
else
{
iconId = AndroidConfig.GetResourceIdByName(notification.Icon);
}

builder.SetSmallIcon(iconId);
}

if (notification.Vibrate)
builder.SetVibrate(new long[] {500, 500});
Expand All @@ -95,7 +111,7 @@ public override Task Send(Notification notification)
if (notification.Sound == Notification.PlatformDefault)
{
// Fallback to the system default notification sound
uri = Android.Media.RingtoneManager.GetDefaultUri(Android.Media.RingtoneType.Notification);
uri = AndroidConfig.DefaultSound;
}
else if (!notification.Sound.Contains("://"))
{
Expand Down
1 change: 1 addition & 0 deletions Plugin.Notifications/Platforms/Uwp/NotificationsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public override Task Send(Notification notification)
{
toastContent.Audio = new ToastAudio
{
// https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio#attributes-and-elements
Src = new Uri("ms-winsoundevent:Notification.Looping.Alarm")
};
}
Expand Down
13 changes: 13 additions & 0 deletions Plugin.Notifications/Platforms/iOS/iOS_Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using UIKit;

namespace Plugin.Notifications.Platforms.iOS
{
public static class Config
{
public static string DefaultIcon { get; set; }
public static string DefaultSound { get; set; }
}
}

0 comments on commit 6c3f805

Please sign in to comment.