Got working (android) notification

This commit is contained in:
BroodjeAap 2020-11-24 19:57:06 +01:00
parent 1f7850e418
commit 3e7f15568e
5 changed files with 137 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 544 B

View file

@ -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: () {

View file

@ -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<ReceivedNotification> didReceiveLocalNotificationSubject =
BehaviorSubject<ReceivedNotification>();
final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();
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<void> 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());
}

View file

@ -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<NotificationsPage> {
@override
Widget build(BuildContext context){
return Scaffold(
drawer: AppDrawer(),
appBar: AppBar(
title: Text("Notifications")
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
FlatButton(
child: Text("Notify!"),
color: Colors.blue,
textColor: Colors.white,
onPressed: _notify
)
],
)
);
}
Future<void> _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?"
);
}
}

View file

@ -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.