Code Monkey home page Code Monkey logo

notificationservice's Introduction

Archived

Please use https://github.com/thudugala/Plugin.LocalNotification for Notifications in Xamarin






Table of Contents

About The Project

NotificationService aims to be a plugin that will simplify the process of adding Notifications within your Xamarin app. Simply provide a time and let the plugin work out the rest under the hood.

Getting Started

Installation

Android Setup

For a base setup, first we need to create our notification channel in MainActivity.cs here is an example

public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public static readonly int NOTIFICATION_ID = 1000; <-- Important that it is 1000 in current plugin
public static readonly string CHANNEL_ID = "YourChannel";
internal static readonly string COUNT_KEY = "count";

        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(savedInstanceState);
            CreateNotificationChannel(); <--- important to call the setup function!

....
void CreateNotificationChannel()
  {
      if (Build.VERSION.SdkInt < BuildVersionCodes.O)
      {
          // Notification channels are new in API 26 (and not a part of the
          // support library). There is no need to create a notification
          // channel on older versions of Android.
          return;
      }

      var name = Resources.GetString(Resource.String.channel_name);
      var description = GetString(Resource.String.channel_description);
      var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Max)
      {
          Description = description,
          LockscreenVisibility = NotificationVisibility.Public,
          Importance = NotificationImportance.Max,

      };

      var notificationManager = (NotificationManager)GetSystemService(NotificationService);
      notificationManager.CreateNotificationChannel(channel);
  }
}

to note, in the current iteration, the plugin will use Application.Context.Resources.GetIdentifier("icon", "mipmap", Application.Context.PackageName) to collect the icon used for Notifications. iOS Setup

By default, if the app is open, iOS will not show a notification, you can choose to override this behaviour with the following

public class NotificationsConfiguration : UNUserNotificationCenterDelegate
{
    public NotificationsConfiguration()
    {
    }

    #region Override Methods
    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // Do something with the notification
        Console.WriteLine("Active Notification: {0}", notification);

        // Tell system to display the notification anyway or use
        // `None` to say we have handled the display locally.
        completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Badge);
    }
    #endregion
}

You may wish to keep it as none if you show notifications internally in the app.

then, in the iOS FinishedLaunching override, add the following

  if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
  {
      // Ask the user for permission to get notifications on iOS 10.0+
      UNUserNotificationCenter.Current.RequestAuthorization(
              UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound | UNAuthorizationOptions.CriticalAlert,
              (approved, error) => { Console.WriteLine(approved); });
  }
  else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
  {
      // Ask the user for permission to get notifications on iOS 8.0+
      var settings = UIUserNotificationSettings.GetSettingsForTypes(
              UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
              new NSSet());

      UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
  }

  UNUserNotificationCenter.Current.Delegate = new NotificationsConfiguration();

Usage

Usage

Current limitations of this plugin is that you can queue only one notification at a time, with new notifications overwriting previous ones, this will be fixed in later versions

the API for usage is as follows

NotificationService.ShowNotification(title: "NotificationTitle",
                                     subtitle: "Quick Notification",
                                      body: "Alarming information",
                                      id: {currentlyUnusedSoJustPut1},
                                      notificationTime: TimeSpan.FromMinutes(30));

This will show the notification in 30 minutes from the current time.

NotificationService.ShowNotification(title: "NotificationTitle",
                                     subtitle: "Quick Notification",
                                      body: "Alarming information",
                                      id: {currentlyUnusedSoJustPut1},
                                      notificationTime: new DateTimeOffset({someTimeHere));

This will show the alarm at {someTimeHere}. Please ensure that this time is in the future.

NotificationService.CancelNotification({justleaveas1})

This will cancel the notification that you have queue'd.

It is setup this way as the future of this plugin involves specifying ID's and having multiple notifications lined up.

PLEASE BE WARNED, Android Doze means that once the app will bunch notifications together to save power. This currently does not affect this plugin as you can only queue one notification, but in the future this may become an issue

License

This project uses the MIT License

Contact

My Github, or reach me on the Xamarin Slack, or on my E-mail

Project Link: NotificationService

Acknowledgements

notificationservice's People

Contributors

codacy-badger avatar dependabot-preview[bot] avatar luckyducko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.