3
votes

I have implemented a code to send Local Notifications for a particular date and time on Android and iOS platform.And, My code is working for Android and iOS platform.In Android,I am using AlarmManager. and in iOS, UILocationNotification for sending notifications. But , I didn't find any solution for the UWP platform to implement the Local Notification feature using Dependency Service as i got for Android and iOS platform. Is there any solution for sending local notifications for particular date and time for all the platforms including "UWP"(mandatory) in Xamarin.forms????

1

1 Answers

3
votes

Xamarin.UWP local notification not working

If you want to make Local notification fore Xamarin.Forms, please refer this document. But it has not implement UWP platform. So we need implement schedule notification within uwp and use dependency service to call it. And I have implement a simple you could refer.

interface

public interface INotificationManager
{ 
    int ScheduleNotification(string title, string message,DateTime scheduledTime);   
}

Implement

public class UWPNotificationManager : INotificationManager
{

    int messageId = -1;

    public int ScheduleNotification(string title, string message,DateTime scheduledTime)
    {

        messageId++;
        string TOAST = $@"<toast>
                  <visual>
                    <binding template='ToastGeneric'>
                      <text>{title}</text>
                      <text>{message}</text>
                    </binding>
                  </visual>
                  <audio src =""ms-winsoundevent:Notification.Mail"" loop=""true""/>
                </toast>";

        Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument();
        xml.LoadXml(TOAST);

        ScheduledToastNotification toast = new ScheduledToastNotification(xml, scheduledTime);
        toast.Id = "IdTostone" + messageId.ToString();
        toast.Tag = "NotificationOne";
        toast.Group = nameof(UWPNotificationManager);
        ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);

        return messageId;
    }

}

Usage

private void OnScheduleClick(object sender, EventArgs e)
{
    notificationNumber++;
    string title = $"Local Notification #{notificationNumber}";
    string message = $"You have now received {notificationNumber} notifications!";
    notificationManager.ScheduleNotification(title, message,DateTime.Now + TimeSpan.FromSeconds(3));
}