Skip to main content

Flutter Alarm Manager Examples

Flutter Alarm Manager: How To Examples using flutter_alarm_manager.

Alarm Manager is a useful plugin in Flutter that allows you to schedule alarms and execute code at specific intervals or at a specific time. In this tutorial, we will explore various examples of using the Flutter Alarm Manager plugin to create alarms and handle them.

Before we begin, make sure you have the Flutter Alarm Manager plugin installed in your Flutter project. You can add it to your pubspec.yaml file:

dependencies:
flutter_alarm_manager: ^0.4.1

Now let's dive into some examples.

Example 1: Scheduling a One-time Alarm

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a one-time alarm to trigger after 5 seconds
FlutterAlarmManager.oneShot(
duration: Duration(seconds: 5),
callback: () {
print("Alarm triggered!");
},
);
}

In this example, we import the flutter_alarm_manager package and initialize it using FlutterAlarmManager.initialize(). Then, we schedule a one-time alarm using FlutterAlarmManager.oneShot(), specifying the duration after which the alarm should trigger and the callback function to be executed when the alarm triggers. In this case, the callback function simply prints "Alarm triggered!" to the console.

Example 2: Scheduling a Repeating Alarm

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a repeating alarm to trigger every 1 minute
FlutterAlarmManager.periodic(
interval: Duration(minutes: 1),
callback: () {
print("Repeating alarm triggered!");
},
);
}

In this example, we schedule a repeating alarm using FlutterAlarmManager.periodic(). We specify the interval between each alarm trigger and the callback function to be executed when the alarm triggers. In this case, the callback function prints "Repeating alarm triggered!" to the console.

Example 3: Cancelling an Alarm

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a one-time alarm to trigger after 5 seconds
int alarmId = FlutterAlarmManager.oneShot(
duration: Duration(seconds: 5),
callback: () {
print("Alarm triggered!");
},
);

// Cancel the alarm after 3 seconds
await Future.delayed(Duration(seconds: 3));
FlutterAlarmManager.cancel(alarmId);
}

In this example, we schedule a one-time alarm and store its ID in the alarmId variable. Then, we cancel the alarm after 3 seconds using FlutterAlarmManager.cancel(). This prevents the alarm from triggering when the specified duration is reached.

Example 4: Handling Alarm Arguments

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a one-time alarm with arguments
int alarmId = FlutterAlarmManager.oneShot(
duration: Duration(seconds: 5),
callback: (String argument) {
print("Alarm triggered with argument: $argument");
},
arguments: "Hello World",
);
}

In this example, we schedule a one-time alarm with arguments. We pass a string argument "Hello World" to the callback function using the arguments parameter. The callback function then prints "Alarm triggered with argument: Hello World" to the console.

Example 5: Handling Alarm Cancellation

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a repeating alarm to trigger every 1 minute
int alarmId = FlutterAlarmManager.periodic(
interval: Duration(minutes: 1),
callback: () {
print("Repeating alarm triggered!");
},
);

// Cancel the alarm after 5 minutes
await Future.delayed(Duration(minutes: 5));
FlutterAlarmManager.cancel(alarmId);
}

In this example, we schedule a repeating alarm and store its ID in the alarmId variable. After 5 minutes, we cancel the alarm using FlutterAlarmManager.cancel(). This stops the alarm from triggering further.

Example 6: Handling Background Execution

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a repeating alarm to trigger every 1 minute
FlutterAlarmManager.periodic(
interval: Duration(minutes: 1),
callback: () async {
// Perform background tasks here
print("Repeating alarm triggered!");
},
executeInBackground: true,
);
}

In this example, we schedule a repeating alarm and enable background execution using the executeInBackground parameter set to true. This allows the callback function to execute even when the app is in the background. The callback function simply prints "Repeating alarm triggered!" to the console.

Example 7: Handling App Launch from Alarm

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a one-time alarm to trigger after 5 seconds
FlutterAlarmManager.oneShot(
duration: Duration(seconds: 5),
callback: () async {
// Perform actions on app launch from alarm here
print("App launched from alarm!");
},
wakeup: true,
);
}

In this example, we schedule a one-time alarm and enable the app to wake up when the alarm triggers using the wakeup parameter set to true. This allows the app to launch and execute the callback function even if it was not running at the time of the alarm. The callback function prints "App launched from alarm!" to the console.

Example 8: Handling Alarm Timezone Changes

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a one-time alarm to trigger at a specific time
FlutterAlarmManager.oneShot(
at: DateTime(2022, 1, 1, 12, 0, 0),
callback: () {
print("Alarm triggered!");
},
);
}

In this example, we schedule a one-time alarm to trigger at a specific time using the at parameter. We pass a DateTime object representing the desired alarm time. The callback function prints "Alarm triggered!" to the console when the alarm triggers.

Example 9: Handling Alarm with Custom Actions

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Schedule a one-time alarm to trigger after 5 seconds
FlutterAlarmManager.oneShot(
duration: Duration(seconds: 5),
callback: () {
// Perform custom actions here
print("Alarm triggered!");
},
actions: [
"Action 1",
"Action 2",
"Action 3",
],
);
}

In this example, we schedule a one-time alarm and provide a list of custom actions using the actions parameter. The callback function can then perform different actions based on the provided list. In this case, the callback function prints "Alarm triggered!" to the console.

Example 10: Handling Alarm with Notification

import 'package:flutter_alarm_manager/flutter_alarm_manager.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void main() async {
// Initialize Flutter Alarm Manager
await FlutterAlarmManager.initialize();

// Initialize Flutter Local Notifications
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.initialize(
InitializationSettings(
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
iOS: IOSInitializationSettings(),
),
);

// Schedule a one-time alarm to trigger after 5 seconds
FlutterAlarmManager.oneShot(
duration: Duration(seconds: 5),
callback: () async {
// Show a notification here
await flutterLocalNotificationsPlugin.show(
0,
"Alarm",
"Alarm triggered!",
NotificationDetails(
android: AndroidNotificationDetails(
'channel_id',
'channel_name',
'channel_description',
importance: Importance.max,
priority: Priority.high,
),
iOS: IOSNotificationDetails(),
),
);
},
);
}

In this example, we schedule a one-time alarm and show a notification when the alarm triggers. We use the Flutter Local Notifications plugin to display the notification. First, we initialize the Flutter Local Notifications plugin. Then, in the alarm callback function, we show the notification using flutterLocalNotificationsPlugin.show(). The notification displays the title "Alarm" and the message "Alarm triggered!".

These examples demonstrate various ways to use the Flutter Alarm Manager plugin to create alarms and handle them. You can customize the code snippets according to your specific requirements and explore more features of the plugin to create powerful alarm functionality in your Flutter apps.

That's it for this tutorial!