I am trying to use the flutter local notification package I write my own code I got code from the internet they both give the same result which is nothing no error, no notification appears, there is nothing
here is the code that I'm using right now
static final _notification = FlutterLocalNotificationsPlugin();
static Future _notificationDetails() async {
return const NotificationDetails(
android: AndroidNotificationDetails(
'Channel id',
'Channel name',
channelDescription: 'Channel description',
importance: Importance.max
),
iOS: IOSNotificationDetails()
);
}
static Future showNotification({
int id =0,
String? title,
String? body,
String? payload
}) async => _notification.show(
id,
title,
body,
await _notificationDetails(),
payload: payload
);
Related
I'm using flutter_local_notification to rended the incoming chat notifications from FCM trying to group messsages from one conversation. The grouping working properly but on receiving a new notification from a different conversation, it overrides and dismiss the previous group.
showNotification({
required int id,
required AndroidNotificationChannel channel,
required String title,
required String body,
required Map<String, dynamic> payload,
}) async {
final androidNotificationDetails = AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
groupKey: channel.groupId,
importance: Importance.max,
priority: Priority.max,
icon: '#mipmap/launcher_icon',
);
final iosNotificationDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
threadIdentifier: channel.groupId, // Notifications with the same threadIdentifier grouped automatically
);
final notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: iosNotificationDetails,
);
_flutterLocalNotificationsPlugin.show(
id,
title,
body,
notificationDetails,
payload: jsonEncode(payload),
);
if (channel.groupId == null) {
return;
}
//* Grouping Messages
List<ActiveNotification> activeNotifications = await _flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!
.getActiveNotifications();
if (!activeNotifications.any((element) => element.groupKey == channel.groupId)) {
return;
}
if (activeNotifications.isNotEmpty) {
// For Android Versions >= 7.0
List<String> lines = activeNotifications.map((e) => e.body.toString()).toList();
InboxStyleInformation inboxStyleInformation = InboxStyleInformation(
lines,
contentTitle: '${lines.length - 1} messages',
summaryText: "${payload["name"].trim()} (${lines.length - 1} messages)",
);
AndroidNotificationDetails androupGroupDetails = AndroidNotificationDetails(
channel.id,
channel.name,
channelDescription: channel.description,
groupKey: channel.groupId,
styleInformation: inboxStyleInformation,
setAsGroupSummary: true,
);
NotificationDetails groupDetails = NotificationDetails(android: androupGroupDetails);
await _flutterLocalNotificationsPlugin.show(0, "", "", groupDetails, payload: jsonEncode(payload));
}
}
}
I've tried to group the same conversation messages in one notification but later on, the group disappeared on receiving a new notification from another user.
I have created NotificationService class which contain method showScheduledNotification() as defined below. I want to call this method to show daily notification at 8 AM with some set of notification subjects but not sure where to call showScheduledNotification method. Here is the Notificaiton Service Class for your reference. Please let me know if additional details are needed.
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
import 'package:rxdart/subjects.dart';
class NotificationService {
static final NotificationService _notificationService =
NotificationService._internal();
final _localNotificationService = FlutterLocalNotificationsPlugin();
final BehaviorSubject<String> onNotificationClick = BehaviorSubject();
factory NotificationService() {
return _notificationService;
}
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
NotificationService._internal();
Future<void> initNotification() async {
final AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('#drawable/ic_launcher');
final InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
tz.initializeTimeZones();
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (payload) => {},
);
}
Future<void> showNotificaiton(
int id, String title, String body, int seconds) async {
tz.setLocalLocation(tz.getLocation('Asia/Kolkata'));
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.now(tz.local).add(Duration(seconds: seconds)),
const NotificationDetails(
android: AndroidNotificationDetails('main_channel', 'main_channel',
importance: Importance.max,
priority: Priority.max,
icon: '#drawable/ic_launcher')),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true);
}
//Show Scheduled LocalNotification
Future<void> showScheduledNotificaitonV2(
int id,
String title,
String body,
) async {
tz.setLocalLocation(tz.getLocation('Asia/Kolkata'));
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.from(DateTime.now().add(Duration(days: 1)), tz.local),
const NotificationDetails(
android: AndroidNotificationDetails('main_channel', 'main_channel',
importance: Importance.max,
priority: Priority.max,
icon: '#drawable/ic_launcher')),
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
androidAllowWhileIdle: true);
}
Future<void> showScheduledNotification(
{int id, String title, String body, int seconds}) async {
final details = await _notificationDetails();
await _localNotificationService.zonedSchedule(
id,
title,
body,
tz.TZDateTime.from(
DateTime.now().add(Duration(seconds: seconds)),
tz.local,
),
details,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
}
Future<NotificationDetails> _notificationDetails() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('channel_id', 'channel_name',
channelDescription: 'description',
importance: Importance.max,
priority: Priority.max,
playSound: true);
const IOSNotificationDetails iosNotificationDetails =
IOSNotificationDetails();
return const NotificationDetails(
android: androidNotificationDetails,
iOS: iosNotificationDetails,
);
}
}
I'm using NotificationDetails to show local notifications in my app:
class NotificationApi {
static final _notifications = FlutterLocalNotificationsPlugin();
static final onNotifications = BehaviorSubject<String?>();
static Future _notificationDetails() async {
return NotificationDetails(
android: AndroidNotificationDetails(
'channel id',
'channel name',
'channel description', //here shows the error
importance: Importance.max,
),
iOS: IOSNotificationDetails(),
);
}
static Future init({bool initScheduled = false}) async {
final android = AndroidInitializationSettings('#mipmap/ic_launcher');
final iOS = IOSInitializationSettings();
final settings = InitializationSettings(android: android, iOS: iOS);
await _notifications.initialize(
settings,
OnSelectNotification: (payload) async {
onNotifications.add(payload);
},
);
}
static Future showNotification({
int id = 0,
String? title,
String? body,
String? payload,
}) async =>
_notifications.show(
id,
title,
body,
await _notificationDetails(),
payload: payload,
);
}
At the moment to implement the third argument in AndroidNotificationDetails(), it is marked as error:
Too many positional arguments: 2 expected, but 3 found.
Try removing the extra positional arguments, or specifying the name for named
arguments.dartextra_positional_arguments_could_be_named
And it shows when i clicked on the () of the method:
I am guiding myself with this tutorial. This code is displayed at minute 2:05
YouTube Video
Channel description is a named parameter, you need to type channelDescription:"Your description"
android: AndroidNotificationDetails(
'channel id',
'channel name',
channelDescription:"Your description",
importance: Importance.max,
),
More about constructors.
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);
}
I used flutter_local_notifications: ^0.7.1+3 in my Flutter app to push schedule notifications. All is well in this but the problem in my notification body is that it shows just one line of text, and I can't expand or stretch notification to show all the notification body text.
This is my try:
class NotificationUtil {
final notifications = FlutterLocalNotificationsPlugin();
final int checkOutNotifyId = 0;
NotificationUtil(BuildContext context) {
final settingsAndroid = AndroidInitializationSettings('ic_notify_icon');
final settingsIOS = IOSInitializationSettings(
onDidReceiveLocalNotification: (id, title, body, payload) =>
onSelectNotification(context));
notifications.initialize(
InitializationSettings(settingsAndroid, settingsIOS),
onSelectNotification: (context) async => onSelectNotification);
}
Future<void> showCheckOutNotify([int maximumCheckoutHours]) async {
await notifications.periodicallyShow(
checkOutNotifyId,
AttendanceConstants.SCHEDULE_NOTIFICATION_TITLE,
AttendanceConstants.SCHEDULE_NOTIFICATION_BODY +
'$maximumCheckoutHours Hour/s of your attendance',
RepeatInterval.Hourly,
_ongoing);
}
NotificationDetails get _ongoing {
final androidChannelSpecifics = AndroidNotificationDetails(
'your channel id',
'your channel name',
'your channel description',
importance: Importance.Max,
priority: Priority.High,
ongoing: true,
);
final iOSChannelSpecifics = IOSNotificationDetails();
return NotificationDetails(androidChannelSpecifics, iOSChannelSpecifics);
}
add [ BigTextStyleInformation('') ] in [ AndroidNotificationDetails() ]
NotificationDetails get _ongoing {
final androidChannelSpecifics = AndroidNotificationDetails(
'your channel id',
'your channel name',
'your channel description',
importance: Importance.Max,
priority: Priority.High,
ongoing: true,
styleInformation: BigTextStyleInformation(''),
);
If anybody is here for the package awesome_notifications, use:
notificationLayout: NotificationLayout.BigText,
inside notification content.