我正在开发一个Flutter应用程序,它需要用户权限才能获得通知。该应用程序旨在具有一个带有按钮的主屏幕,当按下此按钮时,屏幕上应显示通知。但是,我遇到了这个特定功能的问题,当我按下按钮时,通知不会出现。
我已经实现了处理通知和用户权限所需的依赖项。我相信问题可能与按钮被按下时触发通知的代码有关。我正在寻求有关如何正确实现此功能并确保通知按预期显示的指导。
我使用的依赖项是flutter_local_notifications:^8.0.0和permission_handler:^10.2.0,沿着Dart 3.0.6。我无意更改任何依赖项版本。
如果您能在故障排除和解决此问题方面提供任何见解或帮助,我将不胜感激。提前感谢您的帮助!
//**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条答案
按热度按时间qnyhuwrf1#
请仔细检查通知图标的配置是否正确。
@mipmap/ic_launcher
。在调用show notification方法时,还请包含必要的调试日志。sczxawaw2#
好消息!我已经成功地解决了这个问题,做了一些重要的更新。通过切换到'flutter_local_notifications' 9.0.0版本,并巧妙地注解掉'showNotification()'方法中的频道描述行,以符合新版本的要求,我已经设法使一切功能完美无瑕。现在,按下按钮后,屏幕顶部会出现一个无缝通知。
虽然我不完全确定版本更改有效性背后的根本原因,但重要的是它起作用了!我感谢每一个付出时间来帮助我解决这个问题的人的努力。