Essentials: Add Local Notifications

Created on 10 May 2018  路  18Comments  路  Source: xamarin/Essentials

Use case: Showing local notifications in Xamarin Forms projects.

https://github.com/edsnider/LocalNotificationsPlugin

VS bug #735671

feature-request needs-more-discussion proposal up-for-grabs

Most helpful comment

Will leave it as proposal with more discussion to be had.

All 18 comments

For Xamarin.Essentials we are going to keep away from any UI specific features at this time. Just a lot of dependencies and issues around how to handle navigation cross-platform. Thanks for the feature request though and we will see if it evolves over time.

@jamesmontemagno @Redth I was looking at this issue and the plugin and this _may_ be something we want to do. These local notifications to the system, outside the app may be useful and they are very different across platforms.

With regards to the "UI specific" features. Sure, this is UI. But then we have the browser, SMS, dialer, email... The data transfer also shows a popup. I think we need to re evaluate the idea of UI with Essentials as many things have UI. It may be better to see that we don't want a "UI component" (such as a widget or button) and if it has a UI, it must be self-contained.

Does this make sense?

Will leave it as proposal with more discussion to be had.

I think that this would be a great addition.
Now I'm looking for a suitable library for this, for example
https://github.com/aritchie/notifications or https://github.com/edsnider/LocalNotificationsPlugin
and I think it would be good if such a basic functional would be part of Xamarin.Essentials

To do this, you would need the addition of "Xamarin.Android.Support.v4". Taking in additional support libs at this point isn't a big deal since any one of them basically brings the entire suite one way or another.

However, in order to make the API really decent, you should be able to pull up a list of scheduled notifications. In Android, there is no API to do this. I deal with this by creating a reference to sqlite and storing the metadata internally. This also allows the user to clear the list when a user logs out of an application.

A fallback plan is to simply return an ID of the notification that was registered and let the user deal with how they want to store notification data.

I think that this question is only about showing notification bubble. It can be more about badges on the application icon. @aritchie suggested a good idea with store notification data.

I have attempted to handle notifications and scheduling and it is really a mess especially with icons as that is a different API. This probably needs to be broken down into multiple issues and then mocked up as to what each platform can actually do and what it would like like.

Can I try to make a fork and make a notifications?

We recommend not working on anything until an API is specified and the feature is fully fleshed out and in the ready to implement state. At this point I am still unsure what this would even entail or what the API would look like. Start there.

Description of the Feature

Notification center messaging - not to be confused with toast messaging within application

API Declaration

class Notifications
{
    /// <summary>
    /// Set the icon resource Id
    /// </summary>
    static int? AndroidIconResourceId { get; set; }

    /// <summary>
    /// This is required on iOS to trigger permission request
    /// </summary>
    Task<bool> RequestPermission();


    /// <summary>
    /// Get scheduled notifications
    /// </summary>
    /// <returns></returns>
    Task<IEnumerable<Notification>> GetScheduledNotifications();


    /// <summary>
    /// Cancel all scheduled notifications
    /// </summary>
    Task CancelAll();


    /// <summary>
    /// Cancel a specific notification
    /// </summary>
    /// <param name="notificationId"></param>
    /// <returns>Returns true if message found and cancelled successfully</returns>
    Task Cancel(int notificationId);


    /// <summary>
    /// Send a notification
    /// </summary>
    /// <param name="notification"></param>
    /// <returns>The messageID that you can use to cancel with</returns>
    Task Send(Notification notification);
}


public class Notification
{
    public static string DefaultTitle { get; set; }
    public static string DefaultSound { 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 IDictionary<string, string> Metadata { get; set; } = new Dictionary<string, string>();

   /// <summary>
    /// Scheduled Date - null means now
    /// </summary>
    public DateTime? ScheduledDate { get; set; }
}

Documentation links for supported platforms

Current Implementation - https://github.com/aritchie/notifications/tree/dev

Long Term Roadmap

  • event EventHandler Activated - fired when notification is seleected. Current problems with this implementation. The event has to be hooked so early in your application lifecycle that it is almost best to leave this management to the application's platform code.
  • Repeated Intervals (Annual, Weekly, Daily) - Couldn't find a good abstraction point between all of the platforms for this
  • Location Based Notifications - Only offered by Android
  • Replies from Notification - Customization and vastly differing APIs for this at the moment
  • Notification Actions - Customization issues between platforms

Beyond a simple mechanism for showing (and _maybe_ scheduling) and canceling a local notification I don't think this is a good fit for Essentials... there are too many differences (icons, sounds, handling, categories etc.) across the platforms as you get beyond the basic APIs.

for example, I need to show that a new message has come, why do I need a separate plug-in for this? For me it looks like part of the cross-platform funcilonal.
resources with sound and icons can be sent as a parameter, for example I had problems with the notification plugin because it was looking for the application icon on the wrong path.

@aritchie how do you think that if move "int? AndroidIconResourceId {get; set;}" from static Notification and add an icon to the Notification class?

The static makes it appear as configuration at an android level which is fine. The variable at the instance level now makes it android specific in the xplat side of the equation. The goal isn't to cover all of the customization cases per platform. The goal is to hit as many cross platform intersection points.

The purposed spec is what I already have implementations for that work.

Can an optional actionable button be added to the Notification class?

What would the expected action path be for a button on the Notification? I believe the platforms all support this?

I started a very rough implementation of parts of this:
https://github.com/xamarin/Essentials/pull/739

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TedMobile picture TedMobile  路  3Comments

noxe picture noxe  路  3Comments

tsjdev-apps picture tsjdev-apps  路  4Comments

gabry90 picture gabry90  路  3Comments

scotru picture scotru  路  4Comments