-
-
Save kururu-abdo/8cfcc98de9b75b371ba3df193dfb0981 to your computer and use it in GitHub Desktop.
Flutter firebase_messaging + flutter_local_notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:app_name/bloc/authentication/authentication_bloc.dart'; | |
import 'package:app_name/services/local_notification.dart'; | |
import 'package:app_name/view/authentication/AuthenticationInit.dart'; | |
import 'package:app_name/view/authentication/login.dart'; | |
import 'package:app_name/view/main.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:firebase_messaging/firebase_messaging.dart'; | |
import 'bloc/blocList.dart'; | |
Future<dynamic> onBackgroundMessage(Map<String, dynamic> message) { | |
return LocalNotification.showNotification(message); | |
} | |
class MainApp extends StatefulWidget { | |
// This widget is the root of your application. | |
@override | |
_MainAppState createState() => _MainAppState(); | |
} | |
class _MainAppState extends State<MainApp> { | |
final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator"); | |
/// Handling notification message on background. | |
_MainAppState() { | |
/** | |
* FCM Service Register on Main App State | |
*/ | |
FirebaseMessaging.onMessage.listen((RemoteMessage message) { | |
LocalNotification.showNotification(message); | |
}); | |
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { | |
LocalNotification.showNotification(message); | |
}); | |
FirebaseMessaging.onBackgroundMessage(onBackgroundMessage); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator"); | |
return Container( | |
color: Colors.white, | |
child: MultiBlocProvider( | |
providers: BlocList().generate(context), | |
child: BlocBuilder<AuthenticationBloc, AuthenticationState>( | |
builder: (context, state) { | |
if(state is AuthenticationInitial) { | |
context.bloc<AuthenticationBloc>().add(AuthenticationCheck()); | |
return AuthViewInit(); | |
} else if (state is Authenticated) { | |
LocalNotification().notificationHandler(navigatorKey); | |
FirebaseMessaging.instance.subscribeToTopic('pemberitahuan'); | |
if(state.authUser.jabatan.isOffice == 1) { | |
FirebaseMessaging.instance.subscribeToTopic('office'); | |
} else { | |
FirebaseMessaging.instance | |
.subscribeToTopic('outfield-${authPayload.jabatan.id.toString()}'); | |
} | |
return MainView(navigatorKey); | |
} else if (state is Unauthenticated) { | |
FirebaseMessaging.instance.deleteToken(); | |
return MaterialApp( | |
title: 'App Client Manager', | |
home: LoginView(), | |
); | |
} else { | |
return MaterialApp( | |
title: 'App Client Manager', | |
home: Scaffold( | |
body: Center( | |
child: Text('No route defined'), | |
)) | |
); | |
} | |
}, | |
) | |
), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | |
import 'package:flutter/material.dart'; | |
import 'notification_navigate.dart'; | |
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); | |
class LocalNotification { | |
static Future<void> showNotification(RemoteMessage payload) async { | |
final dynamic data = jsonDecode(payload.data['data']); | |
final dynamic notification = jsonDecode(payload.data['notification']); | |
const AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails( | |
'BBD', 'Notification', 'All Notification is Here', | |
importance: Importance.max, | |
priority: Priority.high, | |
ticker: 'ticker'); | |
final int idNotification = data['id'] != null ? int.parse(data['id']) : 1; | |
const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics); | |
await flutterLocalNotificationsPlugin.show( | |
idNotification, notification['title'], notification['body'], platformChannelSpecifics, | |
payload: data['type']); | |
} | |
Future<void> notificationHandler(GlobalKey<NavigatorState> navigatorKey) async { | |
const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('logo_bbd_sm'); | |
final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid); | |
flutterLocalNotificationsPlugin.initialize(initializationSettings, | |
onSelectNotification: (String payload) async { | |
if (payload != null) { | |
NavigatorNavigate().go(navigatorKey, payload); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:bbd_client/app.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:flutter_dotenv/flutter_dotenv.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
import 'package:hive/hive.dart'; | |
Future<void> main() async { | |
/** | |
* | |
* @MainApp() Go to app.dart to see main wrapper. | |
* | |
*/ | |
WidgetsFlutterBinding.ensureInitialized(); | |
await Firebase.initializeApp(); | |
final appDocumentDirectory = await getApplicationDocumentsDirectory(); | |
Hive.init(appDocumentDirectory.path); | |
await DotEnv().load('environment/.env'); | |
runApp( | |
MainApp() | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:app_name/routes.dart'; | |
class MainView extends StatefulWidget { | |
final GlobalKey<NavigatorState> navigatorKey; | |
MainView(this.navigatorKey); | |
@override | |
_MainViewState createState() => _MainViewState(); | |
} | |
class _MainViewState extends State<MainView> with TickerProviderStateMixin { | |
AnimationController _controller; | |
Animation<Offset> _animation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: Duration(milliseconds: 500), | |
vsync: this, | |
)..forward(); | |
_animation = Tween<Offset>( | |
begin: const Offset(0, 50), | |
end: const Offset(0, 0), | |
).animate(CurvedAnimation( | |
parent: _controller, | |
curve: Curves.fastLinearToSlowEaseIn, | |
)); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SlideTransition( | |
position: _animation, | |
child: MaterialApp( | |
title: 'App Client Manager', | |
debugShowCheckedModeBanner: false, | |
navigatorKey: widget.navigatorKey, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
canvasColor: Colors.transparent | |
), | |
initialRoute: '/', | |
onGenerateRoute: Routes().onGenerateRoute, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
best practice to handle notification in flutter
perfect