我将 id 更改为随机生成的数字,但在我安排了一个通知然后另一个通知之后,第一个通知将不会显示。
(由于其他版本问题,我需要这些旧版本)
颤振本地通知:^1.4.4+4
rxdart:^0.24.1
这是触发器:
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
color:
colorChangeNotifyButton ? Colors.grey : Colors.blue,
borderRadius: BorderRadius.circular(15)),
child: MaterialButton(
child: Text("Notify me"),
onPressed: () {
setState(() =>
colorChangeNotifyButton = !colorChangeNotifyButton);
}),
),
),
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(15)),
child: MaterialButton(
child: Text("Submit"),
onPressed: () async {
try {
if (task == null || task.length < 2) {
error(context, "Please add content");
} else {
colorChangeNotifyButton
? null
: await localNotifiyManager.scheduleNotification(
selectedTime,
'Reminder:',
task,
rng.nextInt(100000),
rng.nextInt(100000).toString()
);
print(colorChangeNotifyButton.toString());
setState(() => colorChangeNotifyButton = true);
print('test');
print(colorChangeNotifyButton.toString());
}
} catch (e) {
error(context, e);
}
这是我的 LocalNotifyManager.dart:
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:io' show Platform;
import 'package:rxdart/subjects.dart';
class LocalNotifyManager {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
var initSetting;
BehaviorSubject<ReceivedNotification>
get DidReceiveLocalNotificationSubject =>
BehaviorSubject<ReceivedNotification>();
LocalNotifyManager.init() {
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
if (Platform.isIOS) {
requestIOSPermission();
}
initializePlatform();
}
requestIOSPermission() {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
.requestPermissions(alert: true, badge: true, sound: true);
}
initializePlatform() {
//TODO: need to fix with implementing Notifications for android
var initSettingAndroid = AndroidInitializationSettings('100.png');
var initSettingIOS = IOSInitializationSettings(
requestSoundPermission: true,
requestAlertPermission: true,
requestBadgePermission: true,
onDidReceiveLocalNotification: (id, title, body, payload) async {
ReceivedNotification notification = ReceivedNotification(
id: id, title: title, body: body, payload: payload);
DidReceiveLocalNotificationSubject.add(notification);
});
initSetting = InitializationSettings(initSettingAndroid, initSettingIOS);
}
setOnNotificationReceive(Function onNotificationReceive) {
DidReceiveLocalNotificationSubject.listen((notification) {
onNotificationReceive(notification);
});
}
setOnNotificationClick(Function onNotificationClick) async {
await flutterLocalNotificationsPlugin.initialize(initSetting,
onSelectNotification: (String payload) async {
onNotificationClick(payload);
});
}
Future<void> scheduleNotification(DateTime scheduleNotificationDateTime, String title, String body, int id, String payload) async {
var androidChannel = AndroidNotificationDetails(
'CHANNEL_ID', 'CHANNEL_NAME', 'CHANNEL_DESCRIPTION',
importance: Importance.Max, priority: Priority.High, playSound: true);
var iosChannel = IOSNotificationDetails();
var platformChannel = NotificationDetails(androidChannel, iosChannel);
await flutterLocalNotificationsPlugin.schedule(
id, title, body, scheduleNotificationDateTime, platformChannel,
payload: payload);
}
}
LocalNotifyManager localNotifiyManager = LocalNotifyManager.init();
class ReceivedNotification {
ReceivedNotification({
@required this.id,
@required this.title,
@required this.body,
@required this.payload,
});
final int id;
final String title;
final String body;
final String payload;
}