Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Plugin.LocalNotifications.Abstractions/INotifierService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ public interface ILocalNotifications
/// <param name="id">Id of the notification</param>
void Show(string title, string body, int id = 0);

/// <summary>
/// Show a local notification
/// </summary>
/// <param name="title">Title of the notification</param>
/// <param name="body">Body or description of the notification</param>
/// <param name="id">Id of the notification</param>
/// <param name="backgroundColor">Small icon background color (works only in Android)</param>
/// <param name="smallIcon">Small icon asset name (works only in Android)</param>
/// <param name="largeIcon">Large icon asset name (works only in Android)</param>
void Show(string title, string body, int id = 0, string backgroundColor = null, string smallIcon = null, string largeIcon = null);

/// <summary>
/// Show a local notification at a specified time
/// </summary>
Expand All @@ -24,6 +35,18 @@ public interface ILocalNotifications
/// <param name="notifyTime">Time to show notification</param>
void Show(string title, string body, int id, DateTime notifyTime);

/// <summary>
/// Show a local notification at a specified time
/// </summary>
/// <param name="title">Title of the notification</param>
/// <param name="body">Body or description of the notification</param>
/// <param name="id">Id of the notification</param>
/// <param name="notifyTime">Time to show notification</param>
/// <param name="backgroundColor">Small icon background color (works only in Android)</param>
/// <param name="smallIcon">Small icon asset name (works only in Android)</param>
/// <param name="largeIcon">Large icon asset name (works only in Android)</param>
void Show(string title, string body, int id, DateTime notifyTime, string backgroundColor = null, string smallIcon = null, string largeIcon = null);

/// <summary>
/// Cancel a local notification
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Plugin.LocalNotifications.Android/LocalNotification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Android.Graphics;

namespace Plugin.LocalNotifications
{
Expand All @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System;
using System.IO;
using System.Xml.Serialization;
using Android.Graphics;
using Android.OS;

namespace Plugin.LocalNotifications
{
Expand All @@ -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);
Expand All @@ -52,12 +53,69 @@ public void Show(string title, string body, int id = 0)
notificationManager.Notify(id, builder.Build());
}

/// <summary>
/// Show a local notification
/// </summary>
/// <param name="title">Title of the notification</param>
/// <param name="body">Body or description of the notification</param>
/// <param name="id">Id of the notification</param>
/// <param name="backgroundColor">Small icon background color (works only in Android)</param>
/// <param name="smallIcon">Small icon asset name (works only in Android)</param>
/// <param name="largeIcon">Large icon asset name (works only in Android)</param>
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);
}

/// <summary>
/// Show a local notification at a specified time
/// </summary>
Expand Down Expand Up @@ -93,6 +151,52 @@ public void Show(string title, string body, int id, DateTime notifyTime)
alarmManager.Set(AlarmType.RtcWakeup, triggerTime, pendingIntent);
}

/// <summary>
/// Show a local notification at a specified time
/// </summary>
/// <param name="title">Title of the notification</param>
/// <param name="body">Body or description of the notification</param>
/// <param name="id">Id of the notification</param>
/// <param name="notifyTime">Time to show notification</param>
/// <param name="backgroundColor">Small icon background color (works only in Android)</param>
/// <param name="smallIcon">Small icon asset name (works only in Android)</param>
/// <param name="largeIcon">Large icon asset name (works only in Android)</param>
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);
}

/// <summary>
/// Cancel a local notification
/// </summary>
Expand Down Expand Up @@ -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);
}
}
}
10 changes: 9 additions & 1 deletion src/Plugin.LocalNotifications.Android/ScheduledAlarmHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Show a local notification at a specified time
/// </summary>
Expand Down Expand Up @@ -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);
}

/// <summary>
/// Cancel a local notification
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Show a local notification at a specified time
/// </summary>
Expand All @@ -30,17 +35,22 @@ public void Show(string title, string body, int id = 0)
/// <param name="notifyTime">Time to show notification</param>
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);
}

/// <summary>
/// Cancel a local notification
/// </summary>
Expand Down