Complete BLoC with Clean Architecture (group chat) Discount !! E-commerce App With Backend Source Code Video and Voice Chatting App Firebase Chatting App Source Code Complete Gym App BLoC State Management Source Code Complete Study App Buy Ticket Booking App Source Code Buy Travel App With Backend Source Code Complete Chat App Udemy Course Special Offer Discount !! Online Learning Course App (BLoC) Online Learning Course App (Riverpod) Online Learning Course App (Getx) Discount !! Shopping App (Provider) Cool Flutter Game Nodejs Flutter Complete App
Here we will cover every thing about flutter local notifications. We will cover here the below topics,
1. Showing notification
2. Notification Icon
3. Custom notification sound
First go ahead and install the plugin
then in the main.dart file put the code below
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState(){
super.initState();
Noti.initialize(flutterLocalNotificationsPlugin);
}
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFF3ac3cb), Color(0xFFf85187)])),
child: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
backgroundColor: Colors.blue.withOpacity(0.5),
),
body: Center(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20)
),
width: 200,
height: 80,
child: ElevatedButton(
onPressed: (){
Noti.showBigTextNotification(title: "New message title", body: "Your long body", fln: flutterLocalNotificationsPlugin);
}, child: Text("click"),
),
),
)),
);
}
}
Then create a new class called Noti.dart and put the code below
class Noti{
static Future initialize(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
var androidInitialize = new AndroidInitializationSettings('mipmap/ic_launcher');
var iOSInitialize = new IOSInitializationSettings();
var initializationsSettings = new InitializationSettings(android: androidInitialize,
iOS: iOSInitialize);
await flutterLocalNotificationsPlugin.initialize(initializationsSettings );
}
static Future showBigTextNotification({var id =0,required String title, required String body,
var payload, required FlutterLocalNotificationsPlugin fln
} ) async {
AndroidNotificationDetails androidPlatformChannelSpecifics =
new AndroidNotificationDetails(
'you_can_name_it_whatever1',
'channel_name',
playSound: true,
sound: RawResourceAndroidNotificationSound('notification'),
importance: Importance.max,
priority: Priority.high,
);
var not= NotificationDetails(android: androidPlatformChannelSpecifics,
iOS: IOSNotificationDetails()
);
await fln.show(0, title, body,not );
}
}