update sound when receive from FCM in foreground, background and terminate
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
// ignore_for_file: use_build_context_synchronously
|
||||
|
||||
import 'dart:developer' as dev;
|
||||
import 'dart:math' as math;
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
@@ -8,15 +6,34 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
class NotificationServices {
|
||||
FirebaseMessaging messaging = FirebaseMessaging.instance;
|
||||
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
||||
|
||||
void firebaseInit(BuildContext context) async {
|
||||
Future<void> initLocalNotifications() async {
|
||||
const AndroidInitializationSettings androidInitializationSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const DarwinInitializationSettings iosInitializationSettings = DarwinInitializationSettings();
|
||||
const InitializationSettings initializationSettings = InitializationSettings(
|
||||
android: androidInitializationSettings,
|
||||
iOS: iosInitializationSettings,
|
||||
);
|
||||
|
||||
await _flutterLocalNotificationsPlugin.initialize(
|
||||
initializationSettings,
|
||||
onDidReceiveNotificationResponse: (NotificationResponse response) {
|
||||
dev.log("Người dùng click thông báo ở foreground với payload: ${response.payload}");
|
||||
handleMessage(response.payload);
|
||||
},
|
||||
);
|
||||
dev.log("Local notifications initialized");
|
||||
}
|
||||
|
||||
void firebaseInit(BuildContext context) {
|
||||
FirebaseMessaging.onMessage.listen((message) {
|
||||
initNotifications(context, message);
|
||||
showNotification(message);
|
||||
dev.log(
|
||||
"Title: ${message.notification!.title}, Body: ${message.notification!.body} from ${message.sentTime}");
|
||||
dev.log("Foreground message payload: ${message.toMap()}");
|
||||
if (WidgetsBinding.instance != null) {
|
||||
showNotification(message);
|
||||
} else {
|
||||
dev.log("App is in background, skipping foreground notification");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -25,80 +42,116 @@ class NotificationServices {
|
||||
return token!;
|
||||
}
|
||||
|
||||
void isTokenRefresh() async {
|
||||
void isTokenRefresh() {
|
||||
messaging.onTokenRefresh.listen((newToken) {
|
||||
dev.log("Refresh Firebase Messaging Token: $newToken");
|
||||
});
|
||||
}
|
||||
|
||||
void initNotifications(BuildContext context, RemoteMessage message) async {
|
||||
var androidInitializationSettings =
|
||||
const AndroidInitializationSettings('app_icon');
|
||||
var iosInitializationSettings = const DarwinInitializationSettings();
|
||||
var initializationSettings = InitializationSettings(
|
||||
android: androidInitializationSettings, iOS: iosInitializationSettings);
|
||||
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
|
||||
onDidReceiveNotificationResponse: (payload) {
|
||||
handleMessage(context, message);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> showNotification(RemoteMessage message) async {
|
||||
AndroidNotificationChannel androidNotificationChannel =
|
||||
AndroidNotificationChannel(
|
||||
math.Random.secure().nextInt(1000000).toString(),
|
||||
'high Important Notification',
|
||||
importance: Importance.max);
|
||||
AndroidNotificationDetails androidNotificationDetails =
|
||||
AndroidNotificationDetails(
|
||||
androidNotificationChannel.id.toString(),
|
||||
androidNotificationChannel.name.toString(),
|
||||
sound: RawResourceAndroidNotificationSound(message.data['sound']),
|
||||
String? title = message.data['title'];
|
||||
String? body = message.data['body'];
|
||||
String type = message.data['type'] ?? "normal";
|
||||
|
||||
if (title == null || body == null) {
|
||||
dev.log("Skipping notification due to missing title or body");
|
||||
return;
|
||||
}
|
||||
|
||||
AndroidNotificationChannel androidNotificationChannel = AndroidNotificationChannel(
|
||||
math.Random.secure().nextInt(1000000).toString(),
|
||||
'High Importance Notification',
|
||||
importance: Importance.max,
|
||||
);
|
||||
|
||||
final androidPlugin = _flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
|
||||
await androidPlugin?.deleteNotificationChannel(androidNotificationChannel.id);
|
||||
|
||||
AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
|
||||
androidNotificationChannel.id,
|
||||
androidNotificationChannel.name,
|
||||
channelDescription: "Channel description",
|
||||
sound: getSound(type),
|
||||
importance: androidNotificationChannel.importance,
|
||||
priority: Priority.high,
|
||||
ticker: 'ticker',
|
||||
actions: type == "warn1"
|
||||
? [
|
||||
const AndroidNotificationAction(
|
||||
"id1",
|
||||
"Hogg xóa được",
|
||||
// true thì khi nhấn vào button sẽ mở giao diện ra
|
||||
showsUserInterface: true,
|
||||
cancelNotification: false,
|
||||
)
|
||||
]
|
||||
: null
|
||||
);
|
||||
|
||||
const DarwinNotificationDetails darwinNotificationDetails =
|
||||
DarwinNotificationDetails(
|
||||
const DarwinNotificationDetails darwinNotificationDetails = DarwinNotificationDetails(
|
||||
presentAlert: true,
|
||||
presentBadge: true,
|
||||
presentBanner: true,
|
||||
presentSound: true,
|
||||
);
|
||||
|
||||
NotificationDetails notificationDetails = NotificationDetails(
|
||||
android: androidNotificationDetails, iOS: darwinNotificationDetails);
|
||||
Future.delayed(Duration.zero, () {
|
||||
_flutterLocalNotificationsPlugin.show(0, message.notification!.title!,
|
||||
message.notification!.body, notificationDetails);
|
||||
});
|
||||
android: androidNotificationDetails,
|
||||
iOS: darwinNotificationDetails,
|
||||
);
|
||||
|
||||
// Truyền payload vào thông báo
|
||||
String payload = message.data['type'] ?? "default";
|
||||
await _flutterLocalNotificationsPlugin.show(
|
||||
math.Random.secure().nextInt(1000000),
|
||||
title,
|
||||
body,
|
||||
notificationDetails,
|
||||
payload: payload,
|
||||
);
|
||||
dev.log("Displayed notification with title: $title, body: $body, type: $type");
|
||||
}
|
||||
|
||||
void handleMessage(BuildContext context, RemoteMessage message) async {
|
||||
if (message.data['type'] == "msj") {
|
||||
// Navigator.push(context,
|
||||
// MaterialPageRoute(builder: (context) => const MessageScreen()));
|
||||
} else if (message.data['type'] == "warn") {
|
||||
// Navigator.push(
|
||||
// context, MaterialPageRoute(builder: (context) => const MapScreen()));
|
||||
AndroidNotificationSound getSound(String type) {
|
||||
if (type == "welcome") {
|
||||
return const RawResourceAndroidNotificationSound("welcome");
|
||||
} else if (type == "success") {
|
||||
return const RawResourceAndroidNotificationSound("success_alert");
|
||||
} else if (type == "warn1") {
|
||||
return const RawResourceAndroidNotificationSound("warning_alarm");
|
||||
} else if (type == "warn2") {
|
||||
return const RawResourceAndroidNotificationSound("new_alarm");
|
||||
} else {
|
||||
dev.log("Not found data");
|
||||
return const RawResourceAndroidNotificationSound("normal");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setupInteractMessage(BuildContext context) async {
|
||||
// When app terminate
|
||||
RemoteMessage? initialMessage =
|
||||
await FirebaseMessaging.instance.getInitialMessage();
|
||||
if (initialMessage != null) {
|
||||
// showNotification(initialMessage)
|
||||
handleMessage(context, initialMessage);
|
||||
}
|
||||
void handleMessage(String? payload) {
|
||||
dev.log("Handling notification tap with payload: $payload");
|
||||
// Thêm logic xử lý khi nhấn thông báo ở đây
|
||||
// Ví dụ: Điều hướng màn hình hoặc xử lý dữ liệu
|
||||
}
|
||||
|
||||
// When app is inBackGround
|
||||
Future<void> setupInteractMessage() async {
|
||||
// Khi app terminated
|
||||
RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
|
||||
|
||||
if (initialMessage != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
try {
|
||||
handleMessage(initialMessage.data['type']);
|
||||
} catch (e, stack) {
|
||||
dev.log("Error handling initial message: $e\n$stack");
|
||||
}
|
||||
});
|
||||
}
|
||||
// Khi app ở background
|
||||
FirebaseMessaging.onMessageOpenedApp.listen((message) {
|
||||
handleMessage(context, message);
|
||||
try {
|
||||
handleMessage(message.data['type']);
|
||||
} catch (e, stack) {
|
||||
dev.log("Error in onMessageOpenedApp: $e\n$stack");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user