I’m working on a Flutter app that requires user permission for notifications. The app is intended to have a home screen with a button, and when this button is pressed, a notification should be displayed on the screen. However, I’m encountering an issue with this particular feature, the notification does not appear when I press the button.
I’ve already implemented the necessary dependencies for handling notifications and user permissions. I believe the problem might be related to the code that triggers the notification when the button is pressed. I’m seeking guidance on how to properly implement this feature and ensure that the notification is displayed as expected.
The dependencies I’m using are, flutter_local_notifications: ^8.0.0 and permission_handler: ^10.2.0, along with Dart 3.0.6. I have no intent of changing any of the dependencies versions.
I’d appreciate any insights or assistance in troubleshooting and resolving this issue. Thank you in advance for your help!
//**main.dart**:
import 'package:flutter/material.dart';
import 'notification_service.dart';
// void main() async {
// // needed to reqests nottification permission.
// WidgetsFlutterBinding.ensureInitialized();
// await NotificationService().init();
// // ==========================================
// runApp(const MyApp());
// }
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize notification service and request permission
final notificationService = NotificationService();
await notificationService.init();
runApp( MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Notification App'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
await NotificationService().showNotification();
},
child: Text('Show Notification'),
),
),
),
);
}
}
//**notification_service.dart**:
// This file will handle the setup and display of notifications.
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:permission_handler/permission_handler.dart';
class NotificationService {
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> init() async {
var initializationSettingsAndroid = const AndroidInitializationSettings('@mipmap/ic_launcher');
const initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);
await _flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
// Request notification permissions
await _requestNotificationPermission();
}
Future<void> _requestNotificationPermission() async {
if (await Permission.notification.request().isGranted) {
// Permission is granted, you can show notifications.
} else {
// Permission is not granted, handle accordingly (e.g., show an explanation dialog).
}
}
Future<void> showNotification() async {
print("Attempting to show notification...");
const androidPlatformChannelSpecifics = AndroidNotificationDetails(
'channel_id', // Change this to a unique channel ID
'Channel Name', // Change this to a descriptive channel name
'Channel Description', // Change this to a descriptive channel description, comment this line for flutter_local_notification ^9.0.0
importance: Importance.high,
priority: Priority.high,
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
);
try {
await _flutterLocalNotificationsPlugin.show(
0, // Change this to a unique notification ID
'Notification Title',
'Notification Body',
platformChannelSpecifics,
payload: 'Custom Payload',
);
print("Notification displayed successfully!");
} catch (e) {
print("Error displaying notification: $e");
}
}
Future<void> onSelectNotification(String? payload) async {
// Handle the tapped notification here (if needed).
}
}
2
Answers
Great news! I've successfully resolved the issue by making some crucial updates. By switching to 'flutter_local_notifications' version 9.0.0 and cleverly commenting out the channel description line in the 'showNotification()' method to align with the new version's requirements, I've managed to make everything function flawlessly. Now, upon pressing the button, a seamless notification gracefully appears at the top of the screen.
Although I'm not entirely certain of the underlying reasons behind the version change's effectiveness, but the essential part is that it worked! I appreciate the efforts of everyone who dedicated their time to help me with the issue.
Please double-check if you have the correct configuration of the notification icon.
@mipmap/ic_launcher
.Also please include the necessary debug logs when you are calling the show notification method.