如何在颤动中添加预定通知
How to add Scheduled notification in flutter
我一直在尝试将计划通知功能添加到我的待办事项应用程序,但我无法这样做,当通知应该出现时,它没有出现并且应用程序崩溃了,我希望在以下时间添加通知单击 todoScreen 中的添加按钮。
任何帮助将不胜感激。
todoScreen link: https://github.com/Rohith-JN/Reminders_App/blob/main/lib/Screens/TodoScreen.dart
通知link:https://github.com/Rohith-JN/Reminders_App/blob/main/lib/notification_service.dart
好的,用 flutter 推送通知非常简单。
将这些 dependencies 添加到 pubspec.yaml:
dependencies:
flutter_local_notifications: ^1.4.2
rxdart: ^0.23.1
特定插件:flutter_local_notifications and rxdart
然后 运行 在终端中执行此命令:
flutter pub get
转到 AndroidManifest.xml 到 /android/app/src/main/
并添加这些行:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
现在,转到 AppDelegate.swift 在 /ios/Runner/
添加:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as ?
UNUserNotificationCenterDelegate
}
在这些行之前:
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
完成了吗?
现在,创建一个 notifications_helper.dart
文件并导入 flutter_local_notifications
和 rxdart packages
。
然后在该文件中添加这些:
class NotificationClass{
final int id;
final String title;
final String body;
final String payload;
NotificationClass({this.id, this.body, this.payload, this.title});
}
同时添加这些 finals
:
final rxSub.BehaviorSubject<NotificationClass> didReceiveLocalNotificationSubject =
rxSub.BehaviorSubject<NotificationClass>();
final rxSub.BehaviorSubject<String> selectNotificationSubject =
rxSub.BehaviorSubject<String>();
现在终于将以下 method
添加到辅助文件中:
Future<void> initNotifications(
notifs.FlutterLocalNotificationsPlugin
notifsPlugin) async {
var initializationSettingsAndroid =
notifs.AndroidInitializationSettings('icon');
var initializationSettingsIOS = notifs.IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification:
(int id, String title, String body, String payload) async {
didReceiveLocalNotificationSubject
.add(NotificationClass(id: id, title: title, body: body, payload: payload));
});
var initializationSettings = notifs.InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
await notifsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
print('notification payload: ' + payload);
}
selectNotificationSubject.add(payload);
});
print("Notifications initialised successfully");
}
iOS的权限请求方法:
void requestIOSPermissions(
notifs.FlutterLocalNotificationsPlugin notifsPlugin) {
notifsPlugin.resolvePlatformSpecificImplementation<notifs.IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}
完成了吗?
现在,预定通知,添加这些:
Future<void> scheduleNotification(
{notifs.FlutterLocalNotificationsPlugin notifsPlugin,
String id,
String title,
String body,
DateTime scheduledTime}) async {
var androidSpecifics = notifs.AndroidNotificationDetails(
id, // This specifies the ID of the Notification
'Scheduled notification', // This specifies the name of the notification channel
'A scheduled notification', //This specifies the description of the channel
icon: 'icon',
);
var iOSSpecifics = notifs.IOSNotificationDetails();
var platformChannelSpecifics = notifs.NotificationDetails(
androidSpecifics, iOSSpecifics);
await notifsPlugin.schedule(0, title, "Scheduled notification",
scheduledTime, platformChannelSpecifics); // This literally schedules the notification
}
现在修改main.dart
文件:
NotificationAppLaunchDetails notifLaunch;
final FlutterLocalNotificationsPlugin notifsPlugin=
FlutterLocalNotificationsPlugin();
现在在主要方法中,添加
notifLaunch = await notifsPlugin.getNotificationAppLaunchDetails();
await initNotifications(notifsPlugin);
requestIOSPermissions(notifsPlugin);
现在主要的事情,
触发预定通知,导入您的 helper file
和 main.dart
:
import '../helpers/notifications_helper.dart';
import '../main.dart';
现在调用 scheduleNotification 方法:
scheduleNotification(
notifsPlugin: notifsPlugin, //Or whatever you've named it in main.dart
id: DateTime.now().toString(),
body: "A scheduled Notification",
scheduledTime: DateTime.now()); //Or whenever you actually want to trigger it
我的朋友,你完成了!
我一直在尝试将计划通知功能添加到我的待办事项应用程序,但我无法这样做,当通知应该出现时,它没有出现并且应用程序崩溃了,我希望在以下时间添加通知单击 todoScreen 中的添加按钮。 任何帮助将不胜感激。
todoScreen link: https://github.com/Rohith-JN/Reminders_App/blob/main/lib/Screens/TodoScreen.dart
通知link:https://github.com/Rohith-JN/Reminders_App/blob/main/lib/notification_service.dart
好的,用 flutter 推送通知非常简单。 将这些 dependencies 添加到 pubspec.yaml:
dependencies:
flutter_local_notifications: ^1.4.2
rxdart: ^0.23.1
特定插件:flutter_local_notifications and rxdart
然后 运行 在终端中执行此命令:
flutter pub get
转到 AndroidManifest.xml 到 /android/app/src/main/
并添加这些行:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
现在,转到 AppDelegate.swift 在 /ios/Runner/
添加:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as ?
UNUserNotificationCenterDelegate
}
在这些行之前:
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
完成了吗?
现在,创建一个 notifications_helper.dart
文件并导入 flutter_local_notifications
和 rxdart packages
。
然后在该文件中添加这些:
class NotificationClass{
final int id;
final String title;
final String body;
final String payload;
NotificationClass({this.id, this.body, this.payload, this.title});
}
同时添加这些 finals
:
final rxSub.BehaviorSubject<NotificationClass> didReceiveLocalNotificationSubject =
rxSub.BehaviorSubject<NotificationClass>();
final rxSub.BehaviorSubject<String> selectNotificationSubject =
rxSub.BehaviorSubject<String>();
现在终于将以下 method
添加到辅助文件中:
Future<void> initNotifications(
notifs.FlutterLocalNotificationsPlugin
notifsPlugin) async {
var initializationSettingsAndroid =
notifs.AndroidInitializationSettings('icon');
var initializationSettingsIOS = notifs.IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification:
(int id, String title, String body, String payload) async {
didReceiveLocalNotificationSubject
.add(NotificationClass(id: id, title: title, body: body, payload: payload));
});
var initializationSettings = notifs.InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
await notifsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
print('notification payload: ' + payload);
}
selectNotificationSubject.add(payload);
});
print("Notifications initialised successfully");
}
iOS的权限请求方法:
void requestIOSPermissions(
notifs.FlutterLocalNotificationsPlugin notifsPlugin) {
notifsPlugin.resolvePlatformSpecificImplementation<notifs.IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}
完成了吗?
现在,预定通知,添加这些:
Future<void> scheduleNotification(
{notifs.FlutterLocalNotificationsPlugin notifsPlugin,
String id,
String title,
String body,
DateTime scheduledTime}) async {
var androidSpecifics = notifs.AndroidNotificationDetails(
id, // This specifies the ID of the Notification
'Scheduled notification', // This specifies the name of the notification channel
'A scheduled notification', //This specifies the description of the channel
icon: 'icon',
);
var iOSSpecifics = notifs.IOSNotificationDetails();
var platformChannelSpecifics = notifs.NotificationDetails(
androidSpecifics, iOSSpecifics);
await notifsPlugin.schedule(0, title, "Scheduled notification",
scheduledTime, platformChannelSpecifics); // This literally schedules the notification
}
现在修改main.dart
文件:
NotificationAppLaunchDetails notifLaunch;
final FlutterLocalNotificationsPlugin notifsPlugin=
FlutterLocalNotificationsPlugin();
现在在主要方法中,添加
notifLaunch = await notifsPlugin.getNotificationAppLaunchDetails();
await initNotifications(notifsPlugin);
requestIOSPermissions(notifsPlugin);
现在主要的事情,
触发预定通知,导入您的 helper file
和 main.dart
:
import '../helpers/notifications_helper.dart';
import '../main.dart';
现在调用 scheduleNotification 方法:
scheduleNotification(
notifsPlugin: notifsPlugin, //Or whatever you've named it in main.dart
id: DateTime.now().toString(),
body: "A scheduled Notification",
scheduledTime: DateTime.now()); //Or whenever you actually want to trigger it
我的朋友,你完成了!