update sound when receive from FCM in foreground, background and terminate

This commit is contained in:
anhtunz
2025-03-07 16:44:55 +07:00
parent 314e32eaa9
commit a6fa3b1572
12 changed files with 185 additions and 67 deletions

View File

@@ -1,6 +1,10 @@
import 'dart:developer';
// ignore_for_file: avoid_print
import 'dart:math' as math;
import 'dart:developer' as dev;
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'notification_bloc.dart';
import '../../product/base/bloc/base_bloc.dart';
import '../../product/services/notification_services.dart';
@@ -14,11 +18,12 @@ class NotificationScreen extends StatefulWidget {
class _NotificationScreenState extends State<NotificationScreen> {
late NotificationBloc notificationBloc;
NotificationServices notificationServices = NotificationServices();
final notificationPlugin = FlutterLocalNotificationsPlugin();
@override
void initState() {
super.initState();
initNotification();
notificationBloc = BlocProvider.of<NotificationBloc>(context);
}
@@ -30,11 +35,43 @@ class _NotificationScreenState extends State<NotificationScreen> {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextButton(
onPressed: () {
log("Token: ${notificationServices.getDeviceToken()}");
},
child: Text("Get Notification Token"))
onPressed: () async {
showNewAlarmSoundNotification("warning_alarm");
dev.log("Da vao day");
},
child: const Text("Show new Alarm Notification"),
),
],
));
}
Future<void> initNotification() async{
const isSettingAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
const initSetting = InitializationSettings(android: isSettingAndroid);
await notificationPlugin.initialize(initSetting);
}
Future<void> showNewAlarmSoundNotification(String sound) 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(sound),
channelDescription: "Channel description",
importance: androidNotificationChannel.importance,
priority: Priority.high,
ticker: 'ticker',
);
final NotificationDetails notificationDetails =
NotificationDetails(android: androidNotificationDetails);
await FlutterLocalNotificationsPlugin().show(0, 'New Notification',
'Sound Notification Example', notificationDetails);
}
}