update notification

This commit is contained in:
Efril
2026-05-12 01:54:24 +07:00
parent 3985611a0e
commit 9b6e9c591d
13 changed files with 232 additions and 47 deletions
+54 -4
View File
@@ -1,15 +1,54 @@
import 'dart:convert';
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:injectable/injectable.dart';
/// Background message handler — must be a top-level function.
/// Firebase must be initialized here for background isolate.
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
debugPrint('[FCM] Background message: ${message.messageId}');
// Show local notification for data-only messages in background
await _showBackgroundNotification(message);
}
/// Standalone local notifications plugin for background isolate use.
final _backgroundLocalNotifications = FlutterLocalNotificationsPlugin();
Future<void> _showBackgroundNotification(RemoteMessage message) async {
const androidInit = AndroidInitializationSettings('@drawable/ic_notification');
await _backgroundLocalNotifications.initialize(
const InitializationSettings(android: androidInit),
);
final notification = message.notification;
// Only show manually for data-only messages (FCM auto-shows notification messages)
if (notification != null) return;
final title = message.data['title'] as String?;
final body = message.data['body'] as String?;
if (title == null && body == null) return;
await _backgroundLocalNotifications.show(
message.hashCode,
title,
body,
const NotificationDetails(
android: AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
importance: Importance.high,
priority: Priority.high,
icon: '@drawable/ic_notification',
),
),
payload: jsonEncode(message.data),
);
}
@lazySingleton
@@ -23,7 +62,10 @@ class FcmService {
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.high,
importance: Importance.max, // max agar banner (heads-up) muncul
playSound: true,
enableVibration: true,
enableLights: true,
);
/// Call this once during app startup (after Firebase.initializeApp).
@@ -124,11 +166,19 @@ class FcmService {
_androidChannel.id,
_androidChannel.name,
channelDescription: _androidChannel.description,
icon: '@mipmap/launcher_icon',
importance: Importance.high,
icon: '@drawable/ic_notification',
importance: Importance.max,
priority: Priority.high,
playSound: true,
enableVibration: true,
// Heads-up notification (banner)
fullScreenIntent: false,
),
iOS: const DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
),
iOS: const DarwinNotificationDetails(),
),
payload: jsonEncode(message.data),
);