Skip to content

Instantly share code, notes, and snippets.

@blaqshyd
Forked from mrmarvelworld/main.dart
Created March 18, 2025 00:39
Show Gist options
  • Select an option

  • Save blaqshyd/506f621b89536ef655262953732d22d5 to your computer and use it in GitHub Desktop.

Select an option

Save blaqshyd/506f621b89536ef655262953732d22d5 to your computer and use it in GitHub Desktop.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await NotificationService.instance.initialize();
//YOUR OTHER CODE CAN COME IN HERE
runApp(const MyApp());
}
// make sure to initialize firebase in your codebase before
// testing out the code
import 'dart:convert';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter/material.dart';
import 'package:pika/main.dart';
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await NotificationService.instance.setupLocalNotifications();
await NotificationService.instance.showNotification(message);
}
class NotificationService {
static final NotificationService _instance = NotificationService._internal();
static NotificationService get instance => _instance;
factory NotificationService() => _instance;
NotificationService._internal();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initialize() async {
// Request permission for iOS
await _firebaseMessaging.requestPermission(
alert: true,
badge: true,
sound: true,
);
// Configure foreground notifications
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
showNotification(message);
});
// Configure background & terminated notification clicks
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
_handleNavigation(message.data);
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
// Handle notifications when app is launched from terminated state
RemoteMessage? initialMessage =
await _firebaseMessaging.getInitialMessage();
if (initialMessage != null) {
_handleNavigation(initialMessage.data);
}
setupLocalNotifications();
}
Future<void> setupLocalNotifications() async {
const AndroidInitializationSettings androidSettings =
AndroidInitializationSettings('@mipmap/ic_launcher');
final DarwinInitializationSettings iOSSettings =
DarwinInitializationSettings();
final InitializationSettings settings = InitializationSettings(
android: androidSettings,
iOS: iOSSettings,
);
_flutterLocalNotificationsPlugin.initialize(
settings,
onDidReceiveNotificationResponse: (NotificationResponse response) {
_handleNavigation(jsonDecode(response.payload!));
},
);
}
Future<void> showNotification(RemoteMessage message) async {
final data = message.data;
final String? sound = data['sound'];
final String title = message.notification?.title ?? "New Notification";
final String body = message.notification?.body ?? "";
AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
channelDescription: 'This channel is used for important notifications.',
importance: Importance.max,
priority: Priority.high,
sound: sound != null ? RawResourceAndroidNotificationSound(sound) : null,
);
DarwinNotificationDetails iOSDetails = DarwinNotificationDetails(
sound: sound != null ? '$sound.wav' : '',
);
NotificationDetails platformDetails = NotificationDetails(
android: androidDetails,
iOS: iOSDetails,
);
await _flutterLocalNotificationsPlugin.show(
0,
title,
body,
platformDetails,
payload: jsonEncode(data),
);
}
void _handleNavigation(Map<String, dynamic> data) {
final String? screenType = data['screenType'];
final String? id = data['id'];
if (screenType != null && id != null) {
// Navigate based on screenType and ID
print(screenType);
Widget screen;
switch (screenType.toLowerCase()) {
case 'user_order_screen':
screen =SizedBox();//depends on developer
break;
default:
screen =SizedBox();//depends on developer
}
_navigateToScreen(screen);
}
}
/// Push the Selected Screen to Navigation Stack
void _navigateToScreen(Widget screen) {
navigatorKey.currentState?.push(
MaterialPageRoute(builder: (context) => screen),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment