78 lines
2.5 KiB
Dart
78 lines
2.5 KiB
Dart
// 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';
|
|
|
|
class NotificationScreen extends StatefulWidget {
|
|
const NotificationScreen({super.key});
|
|
|
|
@override
|
|
State<NotificationScreen> createState() => _NotificationScreenState();
|
|
}
|
|
|
|
class _NotificationScreenState extends State<NotificationScreen> {
|
|
late NotificationBloc notificationBloc;
|
|
final notificationPlugin = FlutterLocalNotificationsPlugin();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
initNotification();
|
|
notificationBloc = BlocProvider.of<NotificationBloc>(context);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
TextButton(
|
|
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);
|
|
}
|
|
}
|