【问题标题】:How to add Scheduled notification in flutter如何在颤振中添加计划通知
【发布时间】:2022-01-23 17:41:59
【问题描述】:

我一直在尝试向我的待办事项应用程序添加计划通知功能,但无法这样做,当通知应该出现时,它没有并且应用程序崩溃,我希望在添加时添加通知点击 todoScreen 中的按钮。 任何帮助将不胜感激。

tod​​oScreen 链接:https://github.com/Rohith-JN/Reminders_App/blob/main/lib/Screens/TodoScreen.dart

通知链接:https://github.com/Rohith-JN/Reminders_App/blob/main/lib/notification_service.dart

【问题讨论】:

  • 您是否将日程表转换为 tz 格式?
  • 是的,你看代码了吗?
  • 崩溃时是否会在控制台中抛出任何错误或其他内容?
  • 它不会抛出异常或任何东西,但会在调试控制台中显示:E/AndroidRuntime(5314): FATAL EXCEPTION: main E/AndroidRuntime(5314): Process: com.example.todo_app, PID: 5314 E/AndroidRuntime(5314): java.lang.RuntimeException: 无法启动接收器

标签: flutter flutter-local-notification


【解决方案1】:

好的,使用颤振推送通知非常简单。 将这些依赖项添加到 pubspec.yaml

dependencies:
  flutter_local_notifications: ^1.4.2
  rxdart: ^0.23.1

特定插件:flutter_local_notificationsrxdart

然后在终端中运行这个命令:

flutter pub get

转到/android/app/src/main/ 中的AndroidManifest.xml 并添加以下行:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

现在,去/ios/Runner/中的AppDelegate.swift添加:

if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as ? 
UNUserNotificationCenterDelegate    
}

在这些行之前:

return super.application(application, didFinishLaunchingWithOptions: launchOptions)

完成了吗?

现在,创建一个notifications_helper.dart 文件并导入flutter_local_notificationsrxdart 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();

现在在 main 方法中,添加

notifLaunch = await notifsPlugin.getNotificationAppLaunchDetails();
await initNotifications(notifsPlugin);
requestIOSPermissions(notifsPlugin);

现在最重要的是, 触发预定通知,导入您的helper filemain.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

我的朋友你已经完成了!?

【讨论】:

  • 我试过了,但没有出现通知
  • 您是否正确检查了所有方法名称?尝试更新小部件和颤振版本。使用最新版本的小部件。另请阅读小部件的文档,可能会更改某些方式。
  • 我决定使用另一个名为 awesome_notifications 的通知插件,它工作正常,但不知道如何为每个待办事项唯一地发送通知
猜你喜欢
  • 2021-07-18
  • 1970-01-01
  • 2020-12-27
  • 2021-04-28
  • 2021-06-05
  • 2019-02-26
  • 2020-05-14
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多