import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
void main() async {
// // Initialize Firebase
// WidgetsFlutterBinding.ensureInitialized();
// await Firebase.initializeApp();
// // Create notification channel
// const AndroidNotificationChannel channel = AndroidNotificationChannel(
// 'high_importance_channel',
// 'High Importance Notifications',
// description: 'This channel is used for important notifications.',
// importance: Importance.high,
// );
// await FlutterLocalNotificationsPlugin()
// .resolvePlatformSpecificImplementation<
// AndroidFlutterLocalNotificationsPlugin>()
// ?.createNotificationChannel(channel);
// Run the app
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String? token;
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async{
await Firebase.initializeApp();
}
static const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.high,
);
@override
void initState() {
super.initState();
// Initialize local notifications
var initializationSettingsAndroid =
const AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
FlutterLocalNotificationsPlugin().initialize(initializationSettings);
// Set up message handling
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(
'high_importance_channel',
'High Importance Notifications',
channelDescription:
'This channel is used for important notifications.',
importance: Importance.high,
color: Colors.blue,
icon: '@mipmap/ic_launcher',
),
),
);
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text(notification.title ?? ''),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(notification.body ?? '')],
),
),
);
},
);
}
});
// Get FCM token
getToken();
}
// Function to get FCM token
getToken() async {
token = await FirebaseMessaging.instance.getToken();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('JURNAL11_1302204029'),
),
body: Center(
child: Text(''),
),
),
);
}
}