Code Monkey home page Code Monkey logo

flutter_local_notifications's Introduction

Flutter Local Notifications plugin

This repository consists hosts the following packages

  • flutter_local_notifications: code for the cross-platform facing plugin used to display local notifications within Flutter applications
  • flutter_local_notifications_platform_interface: the code for the common platform interface

These can be found in the corresponding directories within the same name. Most developers are likely here as they are looking to use the flutter_local_notifications plugin. There is a readme file within each directory with more information.

Usage

To be able to use the plugin inside the app few preparations are required. The plugin have to be initialized. The initialization process is done before the app is running actually so it have to be in the main.dart.

Step 1

Get instance of the plugin and add variabels

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

NotificationAppLaunchDetails notificationAppLaunchDetails;

Step 2

Created streams so that app can respond to notification-related events since the plugin is initialised in the main function. Those classes are coming from rxdart

final BehaviorSubject<ReceivedNotification> didReceiveLocalNotificationSubject = BehaviorSubject<ReceivedNotification>();

final BehaviorSubject<String> selectNotificationSubject = BehaviorSubject<String>();

Step 3

Create the class to get the notifications

class ReceivedNotification {
  final int id;
  final String title;
  final String body;
  final String payload;

  ReceivedNotification({
    @required this.id,
    @required this.title,
    @required this.body,
    @required this.payload,
  });
}

Step 4

Make the main function to be async and to return Future.

Future<void> main() async {
  // needed if you intend to initialize in the `main` function
  WidgetsFlutterBinding.ensureInitialized();

  notificationAppLaunchDetails = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
// Note: The icon is required by Android platform and the file have to be available in the "android" resources drawabels when the application is compiled
  var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
  // Note: permissions aren't requested here just to demonstrate 	that can be done later using the "requestPermissions()" method
  // of the "IOSFlutterLocalNotificationsPlugin" class
  var initializationSettingsIOS = IOSInitializationSettings(
      requestAlertPermission: false,
      requestBadgePermission: false,
      requestSoundPermission: false,
      onDidReceiveLocalNotification:
          (int id, String title, String body, String payload) async {
        didReceiveLocalNotificationSubject.add(ReceivedNotification(
            id: id, title: title, body: body, payload: payload));
      });
  var initializationSettings = InitializationSettings(
      initializationSettingsAndroid, initializationSettingsIOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: (String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
    selectNotificationSubject.add(payload);
  });
  
  //Here you should add the "runApp" code
}`

Issues

If you run into bugs, please raise them on the GitHub repository. Please do not email them to me as GitHub is the appropriate place for them and allows for members of the community to answer questions, particularly if I miss the email. It would also be much appreciated if they could be limited to actual bugs or feature requests. If you're looking at how you could use the plugin to do a particular kind of notification, check the example app provides detailed code samples for each supported feature. Also try to check the README first in case you have missed something e.g. platform-specific setup.

Contributions

Contributions are welcome by submitting a PR for to be reviewed. If it's to add new features, appreciate it if you could try to maintain the architecture or try to improve on it. If you are looking to add platform-specific functionality do not add this to the cross-platform facing API (i.e. the FlutterLocalNotificationsPlugin class. The recommended approaches in this scenario are:

  1. see if it can be passed as platform-specific configuration or
  2. add methods to the platform-specific implementations. For example, on iOS there is an IOSFlutterLocalNotificationsPlugin class. You may notice there's a requestPermissions() method that only exists there

flutter_local_notifications's People

Contributors

byrdkm17 avatar danysz avatar dluksza avatar domesticmouse avatar felixyew avatar gianlucaparadise avatar javiercbk avatar jeffscaturro-aka avatar jjs1015 avatar josh-burton avatar lucaslcode avatar maikub avatar menedev avatar mojtabaghiasi avatar psipaylo avatar riccardoratta avatar rookie17 avatar talmor-guy avatar tarekbazine avatar wanbok avatar

Watchers

 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.