Request Permission for Notification
// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Future requestNotificationPermissions() async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission();
}
flutter_local_notifications: ^15.1.1
Send Local Notification
// Automatic FlutterFlow imports
import '/backend/schema/enums/enums.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Future localNotification(
String? title,
String? content,
) async {
// Send Local Notification only Android
// Initialize the FlutterLocalNotificationsPlugin
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Configure the Android initialization settings
var initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
// Configure the initialization settings for the FlutterLocalNotificationsPlugin
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: null,
macOS: null,
);
// Initialize the FlutterLocalNotificationsPlugin with the initialization settings
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
// Configure the notification details
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'channel_id',
'channel_name',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker',
);
// Configure the notification
var notificationDetails = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: null,
macOS: null,
);
// Show the notification
await flutterLocalNotificationsPlugin.show(
0,
title ?? '', // Use title parameter here
content ?? '', // Use content parameter here
notificationDetails,
payload: 'item x',
);
}
Schedule Notification -
// Automatic FlutterFlow imports
import '/backend/schema/enums/enums.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:intl/intl.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tzdata;
import 'package:timezone/timezone.dart' as tz;
Future<void> scheduleNotification(
String? title,
String? content,
String? time, // Change the type to String for the time parameter
) async {
// Print the provided time for debugging
print('Provided time: $time');
await requestNotificationPermissions();
// Parse the time string into a DateTime object
DateTime parsedTime = DateFormat.jm().parse(time!);
// Initialize the timezone database
tzdata.initializeTimeZones();
// Schedule notification based on the provided time
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Initialize the Android-specific settings for the notification
var androidSettings = AndroidNotificationDetails(
'channel_id',
'channel_name',
importance: Importance.high,
priority: Priority.high,
icon:
'@mipmap/ic_launcher', // Replace with your small icon name without the extension
);
// Initialize the notification details
var notificationDetails =
NotificationDetails(android: androidSettings, iOS: null);
// Get the device's timezone
var deviceTimeZone = tz.local;
// Convert the provided time to the device's timezone
var scheduledTime = tz.TZDateTime.from(parsedTime, deviceTimeZone);
// Schedule the notification
// Schedule the notification
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
title!,
content!,
scheduledTime,
notificationDetails,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
payload: 'Custom_Sound',
repeatInterval: RepeatInterval.daily,
);
}