A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.
flutter_local_notifications: ^16.2.0
A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:intl/intl.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Task Timer Notification',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
@override
void initState() {
super.initState();
initializeNotifications();
}
Future<void> initializeNotifications() async {
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
);
}
Future<void> showNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your_channel_id',
'Your Channel Name',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker',
);
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
);
await flutterLocalNotificationsPlugin.show(
0,
'Task Timer Notification',
'e - It\'s time for your task!',
platformChannelSpecifics,
payload: "playload",
);
// Update UI
}
Future<void> onSelectNotification(String payload) async {
// Handle notification selection here
print('Notification selected with payload: $payload');
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Task Timer Notification'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'notification',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
ElevatedButton(onPressed: (){
showNotification();
}, child:Text("Notification"))
],
),
),
);
}
}
Comments
Post a Comment