1

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

todoScreen 链接: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

4

1 回答 1

1

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

dependencies:
  flutter_local_notifications: ^1.4.2
  rxdart: ^0.23.1

特定插件:flutter_local_notificationsrxdart

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

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" />

现在,在Add中转到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_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

我的朋友,你完成了!

于 2021-12-22T17:03:22.510 回答