1
0
mirror of https://github.com/nisrulz/flutter-examples.git synced 2026-08-25 01:05:36 +00:00
Files
flutter-examples/push_notifications/lib/main.dart
Nishant Srivastava 5c53498119 refactor: standardize Dart and Android config across all 54 apps
- Bump all apps to sdk >=3.0.0 <4.0.0 and add flutter_lints + a shared
  analysis_options.yaml to every app (flutter analyze is now green repo-wide)
- Fix real compile errors in firebase_google_authentication and image_editor,
  plus deprecated APIs (Matrix4, SvgPicture.color, textScaleFactor),
  missing async 'mounted' guards, and misc lints via dart fix + manual fixes
- Align pubspec 'name' with each folder and Android namespace/applicationId
  with the documented github.nisrulz.* convention
- Convert bmi_calculator from a legacy Flutter module to a standard app
  with a proper android/ scaffold; fix missing android:exported in launcher
  manifests across 22 apps so all Android builds pass
- Untrack generated build files (GeneratedPluginRegistrant,
  .flutter-plugins-dependencies) and ignore them
2026-08-17 17:26:39 +02:00

146 lines
4.0 KiB
Dart

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Push Notification demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String title = "Title will appear here";
String messageData = "Message text will appear here";
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
@override
Future<void> initState() async {
super.initState();
/*
Message that we are getting from firebase will be of form:-
{
notification:{
title:'Custom Title that we send from firebase cloud messaging',
body:'Text of the message will appear here'
},
data:{
Here the extra data like if we include image,etc optional data from firebase cloud messaging
}
}
For sending Push notification go to Grow and then cloud messaging, from there send new message by adding title
and other fields as per requirement
*/
// Request permission (iOS) and listen for messages.
await _firebaseMessaging.requestPermission(
alert: true,
badge: true,
sound: true,
);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
setState(() {
title = message.notification?.title ?? title;
messageData = message.notification?.body ?? messageData;
notification(context, title, messageData);
});
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
setState(() {
title = message.notification?.title ?? title;
messageData = message.notification?.body ?? messageData;
notification(context, title, messageData);
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Push Notification Demo'),
backgroundColor: Colors.green,
centerTitle: true,
),
body: Center(
child: Padding(
padding: const EdgeInsets.only(top: 30.0),
child: Column(
children: <Widget>[
Text(
title,
style: Theme.of(context).textTheme.headlineSmall,
),
SizedBox(
height: 20.0,
),
Text(
messageData,
style: Theme.of(context).textTheme.headlineLarge,
),
],
),
),
),
);
}
// this function will be called when a push notification is recieved and show as alert dialog along with
// title and message body
Future notification(
BuildContext context, String title, String messageText) async {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
buttonPadding: EdgeInsets.all(10.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
),
SizedBox(
height: 15.0,
),
Text(
messageText,
style: TextStyle(fontSize: 16.0),
)
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context), child: Text('Ok'))
],
);
});
}
}