new branch
This commit is contained in:
122
lib/main.dart
122
lib/main.dart
@@ -2,9 +2,11 @@ import 'dart:developer';
|
||||
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'feature/main/main_screen.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:sfm_app/firebase_options.dart';
|
||||
import 'product/services/api_services.dart';
|
||||
import 'product/services/notification_services.dart';
|
||||
import 'product/services/theme_services.dart';
|
||||
@@ -13,12 +15,89 @@ import 'bloc/main_bloc.dart';
|
||||
import 'product/base/bloc/base_bloc.dart';
|
||||
import 'product/constant/navigation/navigation_router.dart';
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
||||
await setupFlutterNotifications();
|
||||
showFlutterNotification(message);
|
||||
// If you're going to use other Firebase services in the background, such as Firestore,
|
||||
// make sure you call `initializeApp` before using other Firebase services.
|
||||
print('Handling a background message ${message.messageId}');
|
||||
}
|
||||
|
||||
|
||||
/// Create a [AndroidNotificationChannel] for heads up notifications
|
||||
late AndroidNotificationChannel channel;
|
||||
|
||||
bool isFlutterLocalNotificationsInitialized = false;
|
||||
|
||||
Future<void> setupFlutterNotifications() async {
|
||||
if (isFlutterLocalNotificationsInitialized) {
|
||||
return;
|
||||
}
|
||||
channel = const AndroidNotificationChannel(
|
||||
'high_importance_channel', // id
|
||||
'High Importance Notifications', // title
|
||||
description:
|
||||
'This channel is used for important notifications.', // description
|
||||
importance: Importance.high,
|
||||
);
|
||||
|
||||
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
||||
|
||||
/// Create an Android Notification Channel.
|
||||
///
|
||||
/// We use this channel in the `AndroidManifest.xml` file to override the
|
||||
/// default FCM channel to enable heads up notifications.
|
||||
await flutterLocalNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.createNotificationChannel(channel);
|
||||
|
||||
/// Update the iOS foreground notification presentation options to allow
|
||||
/// heads up notifications.
|
||||
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
|
||||
alert: true,
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
isFlutterLocalNotificationsInitialized = true;
|
||||
}
|
||||
|
||||
void showFlutterNotification(RemoteMessage message) {
|
||||
RemoteNotification? notification = message.notification;
|
||||
AndroidNotification? android = message.notification?.android;
|
||||
if (notification != null && android != null && !kIsWeb) {
|
||||
flutterLocalNotificationsPlugin.show(
|
||||
notification.hashCode,
|
||||
notification.title,
|
||||
notification.body,
|
||||
NotificationDetails(
|
||||
android: AndroidNotificationDetails(
|
||||
channel.id,
|
||||
channel.name,
|
||||
channelDescription: channel.description,
|
||||
// TODO add a proper drawable resource to android, for now using
|
||||
// one that already exists in example app.
|
||||
icon: 'launch_background',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize the [FlutterLocalNotificationsPlugin] package.
|
||||
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp();
|
||||
FirebaseMessaging
|
||||
.onBackgroundMessage(firebaseMessagingBackgroundHandler);
|
||||
// NotificationServices().setupInteractMessage();
|
||||
// Set the background messaging handler early on, as a named top-level function
|
||||
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
|
||||
|
||||
if (!kIsWeb) {
|
||||
await setupFlutterNotifications();
|
||||
}
|
||||
|
||||
runApp(
|
||||
BlocProvider(
|
||||
@@ -28,15 +107,7 @@ void main() async {
|
||||
);
|
||||
}
|
||||
|
||||
// @pragma('vm:entry-point')
|
||||
// Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||
// log("Full background message payload: ${message.toMap()}");
|
||||
// await Firebase.initializeApp();
|
||||
// final notificationServices = NotificationServices();
|
||||
// await notificationServices.initLocalNotifications();
|
||||
// await notificationServices.showNotification(message);
|
||||
// log("Background message handled: ${message.data['title']}");
|
||||
// }
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@@ -59,7 +130,7 @@ class _MyAppState extends State<MyApp> {
|
||||
late MainBloc mainBloc;
|
||||
LanguageServices languageServices = LanguageServices();
|
||||
ThemeServices themeServices = ThemeServices();
|
||||
final NotificationServices notificationServices = NotificationServices();
|
||||
// final NotificationServices notificationServices = NotificationServices();
|
||||
APIServices apiServices = APIServices();
|
||||
setLocale(Locale locale) {
|
||||
_locale = locale;
|
||||
@@ -70,23 +141,24 @@ class _MyAppState extends State<MyApp> {
|
||||
_themeData = theme;
|
||||
mainBloc.sinkTheme.add(_themeData);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
mainBloc = BlocProvider.of(context);
|
||||
// notificationServices.initLocalNotifications();
|
||||
// notificationServices.firebaseInit(context);
|
||||
// notificationServices.setupInteractMessage();
|
||||
notificationServices.getDeviceToken().then((token){
|
||||
print("Firebase Token: $token");
|
||||
sendNotificationToken(token);
|
||||
});
|
||||
// // notificationServices.initLocalNotifications();
|
||||
// // notificationServices.firebaseInit(context);
|
||||
// // notificationServices.setupInteractMessage();
|
||||
// notificationServices.getDeviceToken().then((token){
|
||||
// print("Firebase Token: $token");
|
||||
// sendNotificationToken(token);
|
||||
// });
|
||||
}
|
||||
|
||||
void sendNotificationToken (String token) async {
|
||||
int statusCode = await apiServices.sendNotificationToken(token);
|
||||
log("Notification Send StatusCode : $statusCode");
|
||||
}
|
||||
// void sendNotificationToken (String token) async {
|
||||
// int statusCode = await apiServices.sendNotificationToken(token);
|
||||
// log("Notification Send StatusCode : $statusCode");
|
||||
// }
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
|
||||
Reference in New Issue
Block a user