diff --git a/src/Plugin.LocalNotifications.Abstractions/INotifierService.cs b/src/Plugin.LocalNotifications.Abstractions/INotifierService.cs index e8f283c..5b1d639 100644 --- a/src/Plugin.LocalNotifications.Abstractions/INotifierService.cs +++ b/src/Plugin.LocalNotifications.Abstractions/INotifierService.cs @@ -15,6 +15,17 @@ public interface ILocalNotifications /// Id of the notification void Show(string title, string body, int id = 0); + /// + /// Show a local notification + /// + /// Title of the notification + /// Body or description of the notification + /// Id of the notification + /// Small icon background color (works only in Android) + /// Small icon asset name (works only in Android) + /// Large icon asset name (works only in Android) + void Show(string title, string body, int id = 0, string backgroundColor = null, string smallIcon = null, string largeIcon = null); + /// /// Show a local notification at a specified time /// @@ -24,6 +35,18 @@ public interface ILocalNotifications /// Time to show notification void Show(string title, string body, int id, DateTime notifyTime); + /// + /// Show a local notification at a specified time + /// + /// Title of the notification + /// Body or description of the notification + /// Id of the notification + /// Time to show notification + /// Small icon background color (works only in Android) + /// Small icon asset name (works only in Android) + /// Large icon asset name (works only in Android) + void Show(string title, string body, int id, DateTime notifyTime, string backgroundColor = null, string smallIcon = null, string largeIcon = null); + /// /// Cancel a local notification /// diff --git a/src/Plugin.LocalNotifications.Android/LocalNotification.cs b/src/Plugin.LocalNotifications.Android/LocalNotification.cs index 703eb41..9553dd2 100644 --- a/src/Plugin.LocalNotifications.Android/LocalNotification.cs +++ b/src/Plugin.LocalNotifications.Android/LocalNotification.cs @@ -1,4 +1,5 @@ using System; +using Android.Graphics; namespace Plugin.LocalNotifications { @@ -9,5 +10,8 @@ public class LocalNotification public int Id { get; set; } public int IconId { get; set; } public DateTime NotifyTime { get; set; } + public Color? BackgroundColor { get; set; } + public int SmallIconId { get; set; } + public int LargeIconId { get; set; } } } \ No newline at end of file diff --git a/src/Plugin.LocalNotifications.Android/LocalNotificationsImplementation.cs b/src/Plugin.LocalNotifications.Android/LocalNotificationsImplementation.cs index fd7ba26..b6eef93 100644 --- a/src/Plugin.LocalNotifications.Android/LocalNotificationsImplementation.cs +++ b/src/Plugin.LocalNotifications.Android/LocalNotificationsImplementation.cs @@ -5,6 +5,8 @@ using System; using System.IO; using System.Xml.Serialization; +using Android.Graphics; +using Android.OS; namespace Plugin.LocalNotifications { @@ -30,7 +32,6 @@ public void Show(string title, string body, int id = 0) builder.SetContentTitle(title); builder.SetContentText(body); builder.SetAutoCancel(true); - if (NotificationIconId != 0) { builder.SetSmallIcon(NotificationIconId); @@ -52,12 +53,69 @@ public void Show(string title, string body, int id = 0) notificationManager.Notify(id, builder.Build()); } + /// + /// Show a local notification + /// + /// Title of the notification + /// Body or description of the notification + /// Id of the notification + /// Small icon background color (works only in Android) + /// Small icon asset name (works only in Android) + /// Large icon asset name (works only in Android) + public void Show(string title, string body, int id = 0, string backgroundColor = null, string smallIcon = null, string largeIcon = null) + { + var builder = new NotificationCompat.Builder(Application.Context); + builder.SetContentTitle(title); + builder.SetContentText(body); + builder.SetAutoCancel(true); + + int smallIconId = !String.IsNullOrEmpty(smallIcon) ? GetDrawableId(smallIcon) : -1; + int largeIconId = !String.IsNullOrEmpty(largeIcon) ? GetDrawableId(largeIcon) : -1; + + if (smallIconId > 0 && !(Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)) + { + builder.SetSmallIcon(smallIconId); + } + else + { + if (NotificationIconId != 0) + { + builder.SetSmallIcon(NotificationIconId); + } + else + { + builder.SetSmallIcon(Resource.Drawable.plugin_lc_smallicon); + } + } + + if (largeIconId > 0 && !(Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)) + { + builder.SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, largeIconId)); + } + + if (!String.IsNullOrEmpty(backgroundColor) && !(Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)) { + builder.SetColor(Color.ParseColor(backgroundColor)); + } + + var resultIntent = GetLauncherActivity(); + resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask); + var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Application.Context); + stackBuilder.AddNextIntent(resultIntent); + var resultPendingIntent = + stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent); + builder.SetContentIntent(resultPendingIntent); + + var notificationManager = NotificationManagerCompat.From(Application.Context); + notificationManager.Notify(id, builder.Build()); + } + public static Intent GetLauncherActivity() { var packageName = Application.Context.PackageName; return Application.Context.PackageManager.GetLaunchIntentForPackage(packageName); } + /// /// Show a local notification at a specified time /// @@ -93,6 +151,52 @@ public void Show(string title, string body, int id, DateTime notifyTime) alarmManager.Set(AlarmType.RtcWakeup, triggerTime, pendingIntent); } + /// + /// Show a local notification at a specified time + /// + /// Title of the notification + /// Body or description of the notification + /// Id of the notification + /// Time to show notification + /// Small icon background color (works only in Android) + /// Small icon asset name (works only in Android) + /// Large icon asset name (works only in Android) + public void Show(string title, string body, int id, DateTime notifyTime, string backgroundColor = null, string smallIcon = null, string largeIcon = null) + { + var intent = CreateIntent(id); + + var localNotification = new LocalNotification(); + localNotification.Title = title; + localNotification.Body = body; + localNotification.Id = id; + localNotification.NotifyTime = notifyTime; + if (NotificationIconId != 0) + { + localNotification.IconId = NotificationIconId; + } + else + { + localNotification.IconId = Resource.Drawable.plugin_lc_smallicon; + } + + localNotification.SmallIconId = !String.IsNullOrEmpty(smallIcon) ? GetDrawableId(smallIcon) : -1; + localNotification.LargeIconId = !String.IsNullOrEmpty(largeIcon) ? GetDrawableId(largeIcon) : -1; + + if (!String.IsNullOrEmpty(backgroundColor)) + localNotification.BackgroundColor = Color.ParseColor(backgroundColor); + else + localNotification.BackgroundColor = null; + + var serializedNotification = SerializeNotification(localNotification); + intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification); + + var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent); + var triggerTime = NotifyTimeInMilliseconds(localNotification.NotifyTime); + var alarmManager = GetAlarmManager(); + + alarmManager.Set(AlarmType.RtcWakeup, triggerTime, pendingIntent); + } + /// /// Cancel a local notification /// @@ -140,5 +244,9 @@ private long NotifyTimeInMilliseconds(DateTime notifyTime) var utcAlarmTimeInMillis = utcTime.AddSeconds(-epochDifference).Ticks / 10000; return utcAlarmTimeInMillis; } + + private int GetDrawableId(string name) { + return Application.Context.Resources.GetIdentifier(name, "drawable", Application.Context.PackageName); + } } } \ No newline at end of file diff --git a/src/Plugin.LocalNotifications.Android/ScheduledAlarmHandler.cs b/src/Plugin.LocalNotifications.Android/ScheduledAlarmHandler.cs index 10d9b89..1e1d8a7 100644 --- a/src/Plugin.LocalNotifications.Android/ScheduledAlarmHandler.cs +++ b/src/Plugin.LocalNotifications.Android/ScheduledAlarmHandler.cs @@ -2,6 +2,8 @@ using System.Xml.Serialization; using Android.App; using Android.Content; +using Android.Graphics; +using Android.OS; using Android.Support.V4.App; namespace Plugin.LocalNotifications @@ -30,9 +32,15 @@ public override void OnReceive(Context context, Intent intent) var builder = new NotificationCompat.Builder(Application.Context) .SetContentTitle(notification.Title) .SetContentText(notification.Body) - .SetSmallIcon(notification.IconId) + // .SetSmallIcon(notification.IconId) .SetAutoCancel(true); + if (notification.SmallIconId > 0 && !(Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)) builder.SetSmallIcon(notification.SmallIconId); + else builder.SetSmallIcon(notification.IconId); + + if (notification.LargeIconId > 0 && !(Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)) builder.SetLargeIcon(BitmapFactory.DecodeResource(Application.Context.Resources, notification.LargeIconId)); + if (notification.BackgroundColor.HasValue && !(Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat)) builder.SetColor(notification.BackgroundColor.Value); + var resultIntent = LocalNotificationsImplementation.GetLauncherActivity(); resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask); var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Application.Context); diff --git a/src/Plugin.LocalNotifications.iOS/LocalNotificationsImplementation.cs b/src/Plugin.LocalNotifications.iOS/LocalNotificationsImplementation.cs index a4e60d0..3dc6991 100644 --- a/src/Plugin.LocalNotifications.iOS/LocalNotificationsImplementation.cs +++ b/src/Plugin.LocalNotifications.iOS/LocalNotificationsImplementation.cs @@ -33,6 +33,11 @@ public void Show(string title, string body, int id = 0) } } + public void Show(string title, string body, int id = 0, string backgroundColor = null, string smallIcon = null, string largeIcon = null) + { + Show(title, body, id); + } + /// /// Show a local notification at a specified time /// @@ -61,6 +66,11 @@ public void Show(string title, string body, int id, DateTime notifyTime) } } + public void Show(string title, string body, int id, DateTime notifyTime, string backgroundColor = null, string smallIcon = null, string largeIcon = null) + { + Show(title, body, id, notifyTime); + } + /// /// Cancel a local notification /// diff --git a/src/Plugin.LocalNotifications.macOS/LocalNotificationsImplementation.cs b/src/Plugin.LocalNotifications.macOS/LocalNotificationsImplementation.cs index 7670290..f7b2a52 100644 --- a/src/Plugin.LocalNotifications.macOS/LocalNotificationsImplementation.cs +++ b/src/Plugin.LocalNotifications.macOS/LocalNotificationsImplementation.cs @@ -21,6 +21,11 @@ public void Show(string title, string body, int id = 0) Show(title, body, id, DateTime.Now); } + public void Show(string title, string body, int id = 0, string backgroundColor = null, string smallIcon = null, string largeIcon = null) + { + Show(title, body, id, DateTime.Now); + } + /// /// Show a local notification at a specified time /// @@ -30,17 +35,22 @@ public void Show(string title, string body, int id = 0) /// Time to show notification public void Show(string title, string body, int id, DateTime notifyTime) { - var notification = new NSUserNotification() - { - Title = title, - InformativeText = body, - Identifier = id.ToString(), - DeliveryDate = (NSDate)notifyTime - }; + var notification = new NSUserNotification() + { + Title = title, + InformativeText = body, + Identifier = id.ToString(), + DeliveryDate = (NSDate)notifyTime + }; - NSUserNotificationCenter.DefaultUserNotificationCenter.ScheduleNotification(notification); + NSUserNotificationCenter.DefaultUserNotificationCenter.ScheduleNotification(notification); } + public void Show(string title, string body, int id, DateTime notifyTime, string backgroundColor = null, string smallIcon = null, string largeIcon = null) + { + Show(title, body, id, notifyTime); + } + /// /// Cancel a local notification ///