Projemde ne yaptım ise firebase_messaging çalıştıramadım. Yardımcı olursanız sevinirim.
pubspec.yaml dosyası
firebase_core: 1.0.0
firebase_messaging: 9.1.3
flutter_local_notifications: 5.0.0+3
main.dart dosyası
import ‘package:flutter/material.dart’;
import ‘package:firebase_core/firebase_core.dart’;
import ‘package:firebase_messaging/firebase_messaging.dart’;
import ‘package:flutter_local_notifications/flutter_local_notifications.dart’;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MaterialApp(
home: AnaGiris(),
routes: rotalar,
theme: new ThemeData(
scaffoldBackgroundColor: const Color(0xFFFFFFFF),
fontFamily: ‘Segoe UI’,
),
debugShowCheckedModeBanner: false,
));
}
class AnaGiris extends StatefulWidget {
@override
_AnaGirisState createState() => _AnaGirisState();
}
class _AnaGirisState extends State<AnaGiris> {
@override
void initState() {
super.initState();
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message ≠ null) {
Navigator.pushNamed(
context,
‘/anasayfa’,
);
}
});
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,
channel.description,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: 'launch_background',
),
));
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('A new onMessageOpenedApp event was published!');
Navigator.pushNamed(
context,
'/anasayfa',
);
});
super.initState();
Future.delayed(Duration(seconds: 1), () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => AnaSayfa()));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(“assets/acilis.png”), fit: BoxFit.cover)),
),
);
}
}
anasayfa.dart dosyası
import ‘package:firebase_core/firebase_core.dart’;
import ‘package:firebase_messaging/firebase_messaging.dart’;
import ‘package:flutter_local_notifications/flutter_local_notifications.dart’;
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print(‘Handling a background message ${message.messageId}’);
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
‘high_importance_channel’, // id
‘High Importance Notifications’, // title
‘This channel is used for important notifications.’, // description
importance: Importance.high,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<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,
);
runApp(AnaSayfa());
}