信息
pubspec.yaml
firebase_messaging: ^8.0.0-dev.10
main.dart
Future<void> main() async {
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(FirebaseMessagingBackgroundHandler);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
runApp(ModularApp(
module: AppModule(),
));
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
'This channel is used for important notifications.',
importance: Importance.high,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
class AppWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
onListen();
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return OrientationBuilder(
builder: (BuildContext context, Orientation orientation) {
return MaterialApp(
initialRoute: "/main",
title: 'Flutter Demo',
);
},
);
});
}
}
onListen 方法
onListen() {
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(//....);
}
});
}