Skip to content

Commit

Permalink
re-implemented my changes
Browse files Browse the repository at this point in the history
  • Loading branch information
red-rj committed Aug 5, 2018
1 parent 0c4dfa0 commit 85fd0fb
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Plugin.Notifications/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> Metadata { get; set; } = new Dictionary<string, string>();

/// <summary>
Expand Down
22 changes: 19 additions & 3 deletions Plugin.Notifications/Platforms/Android/NotificationsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,38 @@ 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)
builder.SetVibrate(new long[] {500, 500});

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();
Expand Down
26 changes: 18 additions & 8 deletions Plugin.Notifications/Platforms/Uwp/NotificationsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
12 changes: 10 additions & 2 deletions Plugin.Notifications/Platforms/iOS/UNNotificationsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions Plugin.Notifications/Platforms/macOS/NotificationsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 85fd0fb

Please sign in to comment.