Step 1: Create Firebase Project.
Step 2: Add json to android -> app -> directory.
Step 3: Add "firebase_core" , "firebase_messaging" & "flutter local notification" on pubspec.ymal & import packages.
Step 4: Add this code on main.dart and Override your nessecery things.
import 'package:flutter/material.dart';
import 'package:dhakarchaka/pages/NextScreen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description: 'This channel is used for important notifications.', // description
importance: Importance.high,
playSound: true);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future
_firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('A bg message just showed up : ${message.messageId}');
}
Future
main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp,DeviceOrientation.portraitDown]);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation
()
?.createNotificationChannel(channel);
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
State
createState() => _MyAppState();
}
class _MyAppState extends State
{ int _counter = 0; @override void initState() { super.initState(); FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification? notification = message.notification; AndroidNotification? android = message.notification?.android; if (notification != null && android != null) { flutterLocalNotificationsPlugin.show( notification.hashCode, notification.title, notification.body, NotificationDetails( android: AndroidNotificationDetails( channel.id, channel.name, color: Colors.blue, playSound: true, icon: '@mipmap/ic_launcher', ), )); } }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { print('A new onMessageOpenedApp event was published!'); RemoteNotification? notification = message.notification; AndroidNotification? android = message.notification?.android; if (notification != null && android != null) { showDialog( context: context, builder: (_) { return AlertDialog( title: Text(notification.title.toString()), content: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [Text(notification.body.toString())], ), ), ); }); } }); } void showNotification() { setState(() { _counter++; }); flutterLocalNotificationsPlugin.show( 0, "Testing $_counter", "How you doin ?", NotificationDetails( android: AndroidNotificationDetails(channel.id, channel.name, importance: Importance.high, color: Colors.blue, playSound: true, icon: '@mipmap/ic_launcher'))); } @override Widget build(BuildContext context) { return MaterialApp( home: NextScreen() ); } }
Note: This code copied from one of my Application. Maybe you need to change something.