Untitled
unknown
plain_text
a year ago
4.0 kB
7
Indexable
Never
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 { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); await FlutterLocalNotificationsPlugin() .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(channel); await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true, ); // Run the app runApp(MyWidget()); } String? token; Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print('Handling a background message ${message.messageId}'); } const AndroidNotificationChannel channel = AndroidNotificationChannel( 'high_importance_channel', 'High Importance Notifications', description: 'This channel is used for important notifications.', importance: Importance.high, ); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @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 Scaffold( appBar: AppBar( title: const Text('JURNAL11_1302204029'), ), body: Center( child: Text(''), ), ); } } class MyWidget extends StatelessWidget { const MyWidget({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: MyApp(), ); } }