add multiple scheduled notifications using flutter - flutter

I've been working on an adhan app using flutter and I'm working on the scheduled notification using flutter_local_notifications package. Using the .zonedSchedule I can only display notification once.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
AndroidInitializationSettings androidInitializationSettings = AndroidInitializationSettings("icon");
IOSInitializationSettings iosInitializationSettings = IOSInitializationSettings();
final InitializationSettings initializationSettings = InitializationSettings(
android: androidInitializationSettings,
iOS: iosInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
and for the Notification detail
var android = AndroidNotificationDetails(
"$id", channel,
importance: Importance.max,
sound: RawResourceAndroidNotificationSound('adhan'),
playSound: true,
);
var ios = IOSNotificationDetails();
var platform = NotificationDetails(android: android, iOS: ios);
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
and used it as
_flutterLocalNotificationsPlugin.zonedSchedule(
1,
"title",
"dhuhr",
tz.TZDateTime.from(prayerTimes.dhuhr!, location),
platform,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation
.absoluteTime,
androidAllowWhileIdle: true,
matchDateTimeComponents: DateTimeComponents.time
);
_flutterLocalNotificationsPlugin.zonedSchedule(
2,
"title",
"Asr",
tz.TZDateTime.from(prayerTimes.asr!, location),
platform,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation
.absoluteTime,
androidAllowWhileIdle: true,
matchDateTimeComponents: DateTimeComponents.time
);
the problem I'm facing is that if the time matches with the current prayer time it works fine. but when it's time for the next schedule it doesn't display anything.
So how can I display notification multiple times in a day?

Related

how to get notification if app is terminated or closed

I want to show notification based on a condition. I tried with flutter local notification package but I was only getting the foreground and background notification. if I close the app i was not having any notification from app.
example:
app is fetching the data from real-time-database firebase and data base is getting the frequency value from hardware, if frequency is greater than 50 then show notification.
if there is any another way to implement, you can also suggest me that
part of the code:
NotificationService notificationsServices = NotificationService();
void initState(){
super.initState();
notificationsServices.intializeNotification();
}
if(_displayTemp>135 || _displayVib>135)
{
notificationsServices.sendN("Alert", _displayMsg);
}
class NotificationService {
final FlutterLocalNotificationsPlugin flutterNotificationsPlugin = FlutterLocalNotificationsPlugin();
final AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('shield');
void intializeNotification() async {
InitializationSettings initializationSettings= InitializationSettings(
android: initializationSettingsAndroid
);
await flutterNotificationsPlugin.initialize(initializationSettings);
}
void sendN(String title,String body) async {
AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
'channelId 2',
'channelName',
importance: Importance.max,
priority: Priority.high,
playSound: true,
//ongoing: true
);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
);
await flutterNotificationsPlugin.show(
0,
title,
body,
notificationDetails
);
}
}

Flutter: Create a pdf file, show download notification and Open it when user click on notification

I am working on a flutter app. I am creating a pdf file in the "Downloads" folder, after creating the file in the "Downloads" folder, showing a notification "Download Completed" using the "local_notification" plugin.
Now I want to show that pdf when the user clicks on the notification. Can anybody guide me on how I can do that?
Below is my code
final android = AndroidNotificationDetails('0', 'Adun Accounts',
channelDescription: 'channel description',
priority: Priority.high,
importance: Importance.max,
icon: '');
final iOS = IOSNotificationDetails();
final platform = NotificationDetails(android: android, iOS: iOS);
await flutterLocalNotificationsPlugin.show(
0, // notification id
fileName,
'Download complete.',
platform);
show() has a parameter called payload , you can pass the path to the pdf file here. Add the plugin open_file in your pubspec.yaml. Then inside main.dart, add the following code
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initNotification() async {
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('notification_icon');
final IOSInitializationSettings initializationSettingsIOS =
IOSInitializationSettings();
final MacOSInitializationSettings initializationSettingsMacOS =
MacOSInitializationSettings();
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
macOS: initializationSettingsMacOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String? payload) {
if (payload != null) OpenFile.open(payload);
});
}
Note: This function should not be inside any class.

Sound and pop-up notifications do not work in flutter

I am using local_notifications plugin in my android flutter app. I do everything according to the documentation and the notifications work but without sound and pop-up windows. Importance and priority set to max. Maybe the reason is in some settings of my phone or its OS (MIUI Global 11.0.3, Android 9 PKQ1)?
Notification code:
FlutterLocalNotificationsPlugin notificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initNotificationPlugin() async {
const AndroidInitializationSettings androidSettings =
AndroidInitializationSettings('ic_launcher');
final IOSInitializationSettings iosSettings =
IOSInitializationSettings();
final initSettings =
InitializationSettings(android: androidSettings, iOS:
iosSettings);
await notificationsPlugin.initialize(initSettings);
}
void showNotification(String title, String body,
DateTime dateTime, int id) async {
var androidDetails = AndroidNotificationDetails(
'notificationChannel', 'channel', 'description',
importance: Importance.max,
priority: Priority.max,
playSound: true,
showWhen: false,
enableVibration: true);
var iosDetails = IOSNotificationDetails();
var details = NotificationDetails(android: androidDetails, iOS:
iosDetails);
await notificationsPlugin.zonedSchedule(
id,
title,
body,
timezone.TZDateTime.from(
dateTime, timezone.getLocation('Europe/Moscow')),
details,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}

flutter_local_notifications not working on ios, but working on android

If I use the show() method of the FlutterLocalNotificationsPlugin then I get a notification on Android but not on ios.
showNotification(
String notificationId, String title, String body, String payload) async {
final AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
"important-notifications",
tr("DEVICE.IMPORTANT_NOTIFICATIONS_NAME"),
tr("DEVICE.IMPORTANT_NOTIFICATIONS_DESCRIPTION"),
importance: Importance.max,
priority: Priority.high,
ledColor: Colors.pink,
ledOffMs: 50,
ledOnMs: 50,
color: Colors.purple,
styleInformation: BigTextStyleInformation(""),
ticker: 'ticker');
final NotificationDetails platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
);
await flutterLocalNotificationsPlugin
.show(0, title, body, platformChannelSpecifics, payload: payload);
}
The solution to this problem is below.
There's a problem with platform specifics in the flutter_local_notifications plugin.
For example, if you use
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
.createNotificationChannel(channel);
inside of your local notification initialization function, then the function will stop at this point if you're not on an android device. This results in not initializing the plugin with ios + android settings.
Solution
To fix this just wrap everything you do with .resolvePlatformSpecificImplementation into a platform check
if (Platform.isAndroid) {
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
.createNotificationChannel(channel);
}
if (Platform.isIOS) {
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}

Simplest way to show notification

Is there any simple way to show notification in Flutter? I have already tried using this plugin:
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
NotificationAppLaunchDetails notificationAppLaunchDetails;
Future<void> _showNotification(String msg) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'696969', 'FCPN', 'FCPN Example',
importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', msg, platformChannelSpecifics,
payload: 'item x');
}
but I have got null pointer
For iOS, you have to configure your Xcode files.
Add the following lines to the didFinishLaunchingWithOptions method in the AppDelegate.m/AppDelegate.swift file of your iOS project
Look at the Readme for flutter_local_notifications on pub.dev for more info.