Update alarm notification when app in foreground, background and terminate
This commit is contained in:
34
lib/product/services/alarm_services.dart
Normal file
34
lib/product/services/alarm_services.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'dart:io';
|
||||
import 'package:alarm/alarm.dart';
|
||||
|
||||
class AlarmServices {
|
||||
Future<void> showAlarm(String title, String body) async {
|
||||
final DateTime now = DateTime.now();
|
||||
final AlarmSettings alarmSettings = AlarmSettings(
|
||||
id: 42,
|
||||
dateTime: now,
|
||||
assetAudioPath: 'assets/sounds/warning_alarm.mp3',
|
||||
loopAudio: true,
|
||||
vibrate: true,
|
||||
warningNotificationOnKill: Platform.isIOS,
|
||||
androidFullScreenIntent: true,
|
||||
allowAlarmOverlap: true,
|
||||
volumeSettings: VolumeSettings.fade(
|
||||
volume: 1.0,
|
||||
fadeDuration: const Duration(seconds: 3),
|
||||
volumeEnforced: true,
|
||||
),
|
||||
notificationSettings: NotificationSettings(
|
||||
title: title,
|
||||
body: body,
|
||||
stopButton: 'Dừng cảnh báo',
|
||||
icon: 'ic_launcher',
|
||||
),
|
||||
);
|
||||
await Alarm.set(alarmSettings: alarmSettings);
|
||||
}
|
||||
|
||||
void cancelAlarm({int id = 42}) async {
|
||||
await Alarm.stop(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user