我尝试使用最新版本的flutter_local_notifications在我的Flutter项目中包含本地通知:^13.0.0,出现以下错误:
参数类型"Future Function(String?)"不能分配给参数类型"void Function(NotificationResponse)"。
onDidReceiveNotificationResponse : onSelectNotification
这是我的课
class LocalNotificationService {
LocalNotificationService();
final _localNotificationService = FlutterLocalNotificationsPlugin();
final BehaviorSubject<String?> onNotificationClick = BehaviorSubject();
Future<void> initialize() async {
tz.initializeTimeZones();
const AndroidInitializationSettings androidInitializationSettings =
AndroidInitializationSettings('@drawable/ic_stat_android');
DarwinInitializationSettings iosInitializationSettings =
DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);
final InitializationSettings settings = InitializationSettings(
android: androidInitializationSettings,
iOS: iosInitializationSettings,
);
await _localNotificationService.initialize(
settings,
onDidReceiveNotificationResponse : onSelectNotification, //here is the error
);
}
Future<NotificationDetails> _notificationDetails() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('channel_id', 'channel_name',
channelDescription: 'description',
importance: Importance.max,
priority: Priority.max,
playSound: true);
const DarwinNotificationDetails iosNotificationDetails =
DarwinNotificationDetails();
return const NotificationDetails(
android: androidNotificationDetails,
iOS: iosNotificationDetails,
);
}
Future<void> showNotification({
required int id,
required String title,
required String body,
}) async {
final details = await _notificationDetails();
await _localNotificationService.show(id, title, body, details);
}
Future<void> showScheduledNotification(
{required int id,
required String title,
required String body,
required 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<void> showNotificationWithPayload(
{required int id,
required String title,
required String body,
required String payload}) async {
final details = await _notificationDetails();
await _localNotificationService.show(id, title, body, details,
payload: payload);
}
void onDidReceiveLocalNotification(
int id, String? title, String? body, String? payload) {
print('id $id');
}
Future<void> onSelectNotification(String? payload) async {
print('payload $payload');
if (payload != null && payload.isNotEmpty) {
onNotificationClick.add(payload);
}
}
}
我错过了什么?
1条答案
按热度按时间q8l4jmvw1#
onDidReceiveNotificationResponse
在回调时提供NotificationResponse
。您可以像这样获得payload
或者
这将使用
onDidReceiveNotificationResponse : onSelectNotification
。