i have developed app which has daily temperature notification as functionality.
I am able to implement scheduled daily notification at specific time, i need to change value of title and body for each time the schedule triggers
Is it possible to implement using flutter local-notification / flutter awesome notification, or should we depend on alarm plugin to trigger local notification
/// IN Main.dart /////
// @pragma('vm:entry-point')
void scheduleBackgroundProcess() async {
await NotificationService.showNotification(
title: 'Temperature $dateToday', ////// need to change this value for each trigger
body: 'Temperature: $derivedTemperature', ////// need to change this value for each trigger
scheduled: true,
);
}
/////// My Notification Service
class NotificationService {
static Future<void> initializeNotification() async {
await AwesomeNotifications().initialize(
null,
[
NotificationChannel(
channelGroupKey: 'high_importance_channel',
channelKey: 'high_importance_channel',
channelName: 'Basic notifications',
channelDescription: 'Notification channel for basic tests',
defaultColor: const Color(0xFF9D50DD),
ledColor: Colors.white,
importance: NotificationImportance.Max,
channelShowBadge: true,
onlyAlertOnce: true,
playSound: true,
criticalAlerts: true,
)
],
channelGroups: [
NotificationChannelGroup(
channelGroupKey: 'high_importance_channel_group',
channelGroupName: 'Group 1',
)
],
debug: true,
);
}
await AwesomeNotifications().isNotificationAllowed().then(
(isAllowed) async {
if (!isAllowed) {
await AwesomeNotifications().requestPermissionToSendNotifications();
}
},
);
await AwesomeNotifications().setListeners(
onActionReceivedMethod: onActionReceivedMethod,
onNotificationCreatedMethod: onNotificationCreatedMethod,
onNotificationDisplayedMethod: onNotificationDisplayedMethod,
onDismissActionReceivedMethod: onDismissActionReceivedMethod,
);
}
/// Use this method to detect when a new notification or a schedule is created
static Future<void> onNotificationCreatedMethod(
ReceivedNotification receivedNotification) async {
debugPrint('onNotificationCreatedMethod');
}
/// Use this method to detect every time that a new notification is displayed
static Future<void> onNotificationDisplayedMethod(
ReceivedNotification receivedNotification) async {
debugPrint('onNotificationDisplayedMethod');
}
/// Use this method to detect if the user dismissed a notification
static Future<void> onDismissActionReceivedMethod(
ReceivedAction receivedAction) async {
debugPrint('onDismissActionReceivedMethod');
}
/// Use this method to detect when the user taps on a notification or action button
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
debugPrint('onActionReceivedMethod');
final payload = receivedAction.payload ?? {};
if (payload["navigate"] == "true") {
MyApp.navigatorKey.currentState?.push(
MaterialPageRoute(
builder: (_) => const homePage(),
),
);
}
}
static Future<void> showNotification({
required final String title,
required final String body,
final String? summary,
final Map<String, String>? payload,
final ActionType actionType = ActionType.Default,
final NotificationLayout notificationLayout = NotificationLayout.Default,
final NotificationCategory? category,
final String? bigPicture,
final List<NotificationActionButton>? actionButtons,
final bool scheduled = false,
final int? interval,
}) async {
assert(!scheduled || (scheduled && interval != null));
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 0,
channelKey: 'high_importance_channel',
title: title, ////// need to change this value for each trigger
body: body, ////// need to change this value for each trigger
actionType: actionType,
summary: summary,
category: category,
payload: payload,
bigPicture: 'asset://assets/bigpicture.png',
largeIcon: 'asset://assets/logonotify.png',
notificationLayout: NotificationLayout.BigPicture,
),
actionButtons: actionButtons,
//// Daily Schedule notification
schedule: scheduled
? NotificationCalendar(
hour: 3,
minute: 30,
allowWhileIdle: true,
repeats: true,
timeZone:
await AwesomeNotifications().getLocalTimeZoneIdentifier(),
preciseAlarm: true)
: null,
);
}
static Future<void> resetBadgeCounter() async {
await AwesomeNotifications().resetGlobalBadge();
}
static Future<void> cancelNotifications() async {
await AwesomeNotifications().cancelAll();
}
`