got full screen notification working, doesn't wake up the device yet, but payload is delivered and right pizza event/recipe step is fetched

This commit is contained in:
broodjeaap89 2021-08-15 21:39:54 +02:00
parent 41b003acb7
commit eb20ae24c0
4 changed files with 104 additions and 20 deletions

View file

@ -39,7 +39,8 @@ class PizzaEvent extends HiveObject{
"PizzaEventChannel", "PizzaEventChannel", "PizzaPlanner notification channel",
importance: Importance.max,
priority: Priority.high,
ticker: "ticker"
ticker: "ticker",
fullScreenIntent: true,
);
const platformChannelSpecific = NotificationDetails(android: androidPlatformChannelSpecifics);
@ -53,20 +54,22 @@ class PizzaEvent extends HiveObject{
final List<PendingNotificationRequest> pendingNotificationRequests = await flutterLocalNotificationsPlugin.pendingNotificationRequests();
int notificationId = pendingNotificationRequests.map((pendingNotification) => pendingNotification.id).fold(0, max);
int stepId = 0;
for (var recipeStep in this.recipe.recipeSteps) {
await flutterLocalNotificationsPlugin.zonedSchedule(
notificationId,
notificationId+stepId,
recipeStep.name,
null,
stepTime,
platformChannelSpecific,
androidAllowWhileIdle: true,
payload: "${this.key}__$stepId",
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime
);
recipeStep.notificationId = notificationId;
stepTime = stepTime.add(Duration(seconds: recipeStep.getCurrentWaitInSeconds()));
notificationId++;
stepId++;
}
}
}

View file

@ -8,17 +8,22 @@ import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
import 'package:pizzaplanner/pages/AddPizzaEventPage.dart';
import 'package:pizzaplanner/pages/PickPizzaRecipePage.dart';
import 'package:pizzaplanner/pages/PizzaEventNotificationPage.dart';
import 'package:pizzaplanner/pages/PizzaEventPage.dart';
import 'package:pizzaplanner/pages/PizzaEventsPage.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:pizzaplanner/util.dart';
import 'package:rxdart/subjects.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final BehaviorSubject<String?> selectNotificationSubject = BehaviorSubject<String?>();
String? selectedNotificationPayload;
void main() async {
// hive init
@ -47,28 +52,51 @@ void main() async {
iOS: initializationSettingsIOS,
macOS: initializationSettingsMacOS
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: selectNotification);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (String? payload) async {
selectedNotificationPayload = payload;
selectNotificationSubject.add(payload);
});
// init timezones properly
tz.initializeTimeZones();
final String? timeZoneName = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(timeZoneName!));
runApp(PizzaPlanner());
runApp(
MaterialApp(
title: "PizzaPlanner",
home: PizzaPlanner(),
onGenerateRoute: RouteGenerator.generateRoute,
)
);
//await Hive.close();
}
class PizzaPlanner extends StatelessWidget {
// This widget is the root of your application.
class PizzaPlanner extends StatefulWidget {
@override
PizzaPlannerState createState() => PizzaPlannerState();
}
class PizzaPlannerState extends State<PizzaPlanner> {
@override
void initState(){
super.initState();
this._configureSelectNotificationSubject();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "PizzaPlanner",
home: PizzaEventsPage(),
onGenerateRoute: RouteGenerator.generateRoute,
);
return PizzaEventsPage();
}
void _configureSelectNotificationSubject() {
selectNotificationSubject.stream.listen((String? payload) async {
await Navigator.pushNamed(context, '/event/notification', arguments: payload);
});
}
}
@ -87,6 +115,9 @@ class RouteGenerator {
case "/event/view": {
return MaterialPageRoute(builder: (context) => PizzaEventPage(settings.arguments as PizzaEvent));
}
case "/event/notification": {
return MaterialPageRoute(builder: (context) => PizzaEventNotificationPage(settings.arguments as String?));
}
default: {
return MaterialPageRoute(builder: (context) => PizzaEventsPage());
@ -95,10 +126,3 @@ class RouteGenerator {
}
}
Future selectNotification(String? payload) async {
if (payload == null) {
print("Payload: null");
return;
}
print("Payload: $payload");
}

View file

@ -0,0 +1,55 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:pizzaplanner/entities/PizzaEvent.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart';
class PizzaEventNotificationPage extends StatefulWidget {
final String? payload;
PizzaEventNotificationPage(this.payload);
@override
PizzaEventNotificationState createState() => PizzaEventNotificationState();
}
class PizzaEventNotificationState extends State<PizzaEventNotificationPage> {
late final PizzaEvent pizzaEvent;
late final RecipeStep recipeStep;
@override
void initState() {
super.initState();
if (this.widget.payload == null){
print("Redirected to notification page but no payload... Popping");
Navigator.pop(context);
}
var split = this.widget.payload!.split("__");
var pizzaEventId = int.parse(split[0]);
var recipeStepId = int.parse(split[1]);
var pizzaEventsBox = Hive.box<PizzaEvent>("PizzaEvents");
pizzaEvent = pizzaEventsBox.get(pizzaEventId)!;
recipeStep = pizzaEvent.recipe.recipeSteps[recipeStepId];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("From notification"),
),
resizeToAvoidBottomInset: false,
body: Container(
padding: EdgeInsets.fromLTRB(40, 10, 40, 10),
child: Column(
children: <Widget>[
Text(pizzaEvent.name),
Text(recipeStep.name)
]
)
)
);
}
}

View file

@ -38,6 +38,8 @@ dependencies:
hive: ^2.0.4
hive_flutter: ^1.1.0
rxdart: ^0.27.1
timezone: ^0.7.0
flutter_native_timezone: ^2.0.0
flutter_local_notifications: ^8.0.0