I’m simply adding a local notification, however in real device notification is appears but without making a sound. For certain real devices, the notification is enabled by default, but we have to manually enable it in some devices. can anyone know how should i add permission in this code
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/timezone.dart' as tz;
class NotificationService {
final FlutterLocalNotificationsPlugin notificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initNotification() async {
AndroidInitializationSettings initializationSettingsAndroid =
const AndroidInitializationSettings('mipmap/ic_launcher');
var initializationSettingsIOS = DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification:
(int id, String? title, String? body, String? payload) async {});
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
await notificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse:
(NotificationResponse notificationResponse) async {});
}
notificationDetails() {
return const NotificationDetails(
android: AndroidNotificationDetails('channelId', 'channelName',
importance: Importance.max,
playSound: true,
),
iOS: DarwinNotificationDetails());
}
Future showNotification(
{int id = 0, String? title, String? body, String? payLoad}) async {
return notificationsPlugin.show(
id, title, body, await notificationDetails());
}
Future scheduleNotification(
{int id = 0,
String? title,
String? body,
String? payLoad,
required DateTime scheduledNotificationDateTime}) async {
return notificationsPlugin.zonedSchedule(
id,
title,
body,
tz.TZDateTime.from(
scheduledNotificationDateTime,
tz.local,
),
await notificationDetails(),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}
}
2
Answers
You can use permission_handler plugin to request permission on iOS, android and windows.
For Example:
if you are using for iOS then don’t forget to add this in your pod file
A more holistic answer.