From 3e7f15568e1b1e2cf6c31ce3bf4d48dff3933d89 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Tue, 24 Nov 2020 19:57:06 +0100 Subject: [PATCH] Got working (android) notification --- .../app/src/main/res/drawable/app_icon.png | Bin 0 -> 544 bytes lib/AppDrawer.dart | 7 ++ lib/main.dart | 78 +++++++++++++++++- lib/pages/NotificationsPage.dart | 48 +++++++++++ pubspec.yaml | 5 ++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 android/app/src/main/res/drawable/app_icon.png create mode 100644 lib/pages/NotificationsPage.dart diff --git a/android/app/src/main/res/drawable/app_icon.png b/android/app/src/main/res/drawable/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..db77bb4b7b0906d62b1847e87f15cdcacf6a4f29 GIT binary patch literal 544 zcmeAS@N?(olHy`uVBq!ia0vp^9w5xY3?!3`olAj~WQl7;NpOBzNqJ&XDuZK6ep0G} zXKrG8YEWuoN@d~6R2!h8bpbvhu0Wd6uZuB!w&u2PAxD2eNXD>P5D~Wn-+_Wa#27Xc zC?Zj|6r#X(-D3u$NCt}(Ms06KgJ4FxJVv{GM)!I~&n8Bnc94O7-Hd)cjDZswgC;Qs zO=b+9!WcT8F?0rF7!Uys2bs@gozCP?z~o%U|N3vA*22NaGQG zlg@K`O_XuxvZ&Ks^m&R!`&1=spLvfx7oGDKDwpwW`#iqdw@AL`7MR}m`rwr|mZgU`8P7SBkL78fFf!WnuYWm$5Z0 zNXhDbCv&49sM544K|?c)WrFfiZvCi9h0O)B3Pgg&ebxsLQ05GG~ AQ2+n{ literal 0 HcmV?d00001 diff --git a/lib/AppDrawer.dart b/lib/AppDrawer.dart index 352e08b..3636bf8 100644 --- a/lib/AppDrawer.dart +++ b/lib/AppDrawer.dart @@ -25,6 +25,13 @@ class AppDrawer extends StatelessWidget { Navigator.pushNamed(context, "/years"); } ), + ListTile( + title: Text("Notifications"), + onTap: () { + Navigator.pop(context); + Navigator.pushNamed(context, "/notifications"); + } + ), ListTile( title: Text("Practice Months"), onTap: () { diff --git a/lib/main.dart b/lib/main.dart index 3e124ef..63c2892 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,12 +1,85 @@ import 'package:flutter/material.dart'; +import 'package:ohthatsa/pages/NotificationsPage.dart'; import 'package:ohthatsa/pages/YearsPage.dart'; import 'package:ohthatsa/pages/practice/PracticeSetup.dart'; import 'package:ohthatsa/pages/practice/PracticeSetupPage.dart'; import 'package:ohthatsa/pages/practice/PracticePage.dart'; import 'package:ohthatsa/pages/MonthValuesPage.dart'; +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:rxdart/subjects.dart'; +import 'package:flutter/services.dart'; + +final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = + FlutterLocalNotificationsPlugin(); + +final BehaviorSubject didReceiveLocalNotificationSubject = + BehaviorSubject(); + +final BehaviorSubject selectNotificationSubject = + BehaviorSubject(); + +const MethodChannel platform = + MethodChannel('dexterx.dev/flutter_local_notifications_example'); + +class ReceivedNotification { + ReceivedNotification({ + @required this.id, + @required this.title, + @required this.body, + @required this.payload, + }); + + final int id; + final String title; + final String body; + final String payload; +} + + +Future main() async { + + // Notification stuff + WidgetsFlutterBinding.ensureInitialized(); + + final NotificationAppLaunchDetails notificationAppLaunchDetails = + await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails(); + + const AndroidInitializationSettings initializationSettingsAndroid = + AndroidInitializationSettings('app_icon'); + + final IOSInitializationSettings 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)); + }); + const MacOSInitializationSettings initializationSettingsMacOS = + MacOSInitializationSettings( + requestAlertPermission: false, + requestBadgePermission: false, + requestSoundPermission: false + ); + final InitializationSettings initializationSettings = InitializationSettings( + android: initializationSettingsAndroid, + iOS: initializationSettingsIOS, + macOS: initializationSettingsMacOS + ); + await flutterLocalNotificationsPlugin.initialize( + initializationSettings, + onSelectNotification: (String payload) async { + if (payload != null) { + debugPrint('notification payload: $payload'); + } + selectNotificationSubject.add(payload); + } + ); + // end of notification stuff the fuck -void main() { runApp(OhThatsA()); } @@ -36,6 +109,9 @@ class RouteGenerator { case "/practice/practice": { return MaterialPageRoute(builder: (context) => PracticePage(settings.arguments)); } + case "/notifications": { + return MaterialPageRoute(builder: (context) => NotificationsPage()); + } default: { return MaterialPageRoute(builder: (context) => YearsPage()); } diff --git a/lib/pages/NotificationsPage.dart b/lib/pages/NotificationsPage.dart new file mode 100644 index 0000000..c941394 --- /dev/null +++ b/lib/pages/NotificationsPage.dart @@ -0,0 +1,48 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_local_notifications/flutter_local_notifications.dart'; +import 'package:ohthatsa/AppDrawer.dart'; +import 'package:ohthatsa/main.dart'; + +class NotificationsPage extends StatefulWidget { + @override + _NotificationPageState createState() => _NotificationPageState(); +} + +class _NotificationPageState extends State { + @override + Widget build(BuildContext context){ + return Scaffold( + drawer: AppDrawer(), + appBar: AppBar( + title: Text("Notifications") + ), + body: Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + FlatButton( + child: Text("Notify!"), + color: Colors.blue, + textColor: Colors.white, + onPressed: _notify + ) + ], + ) + ); + } + + Future _notify() async { + const AndroidNotificationDetails androidPlatformChannelSpecifics = + AndroidNotificationDetails("test", "test", "test", ticker: 'ticker'); + const NotificationDetails platformChannelSpecifics = + NotificationDetails(android: androidPlatformChannelSpecifics); + await flutterLocalNotificationsPlugin.show( + 0, + 'Title!', + 'and the body...', + platformChannelSpecifics, + payload: "test item?" + ); + + } +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 2a21c50..8f5519e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,6 +27,11 @@ dependencies: sqflite: ^1.3.0 intl: ^0.16.1 + # Notification libs + rxdart: ^0.24.0 + flutter_local_notifications: ^3.0.1+5 + + # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.