0

不,我不是在这里询问 Firebase Cloud Messaging 通知,而是纯粹使用来自这里的本地通知包颤动本地通知

我希望如果我的用户单击通知,那么它将被定向到某个页面。怎么做 ?

目前我的代码是这样的

class LocalNotificationService  {
  late FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin;
  LocalNotificationService._privateConstructor() {
    _init();
  }

  static final LocalNotificationService _instance = LocalNotificationService._privateConstructor();
  static LocalNotificationService get instance => _instance; // singleton access

  void _init() {
    _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    _initializePlatformSpecifics();
  }

  Future<void> _initializePlatformSpecifics() async {
    var androidInitializationSettings = AndroidInitializationSettings("app_notification_icon");
    var iOSInitializationSettings = IOSInitializationSettings(
      requestAlertPermission: true,
      requestBadgePermission: true,
      requestSoundPermission: true,
      onDidReceiveLocalNotification: (id, title, body, payload) async {
        // do something if notification is clicked
      },
    );

    final initializationSettings = InitializationSettings(
      android: androidInitializationSettings,
      iOS: iOSInitializationSettings,
    );

    await _flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

  void showSimpleNotification({
    required String title,
    required String body,
    required NotificationType notificationType,
  }) async {
    var android = _setAndroidNotificationDetails(notificationType);
    var ios = IOSNotificationDetails();

    var notificationDetails = new NotificationDetails(android: android, iOS: ios);

    await _flutterLocalNotificationsPlugin.show(
      1, // only need to show one notification in the notification tray, so it is ok if we use the Notification ID
      title,
      body,
      notificationDetails,
    );
  }

  
}
4

1 回答 1

0

我建议阅读插件文档

根据文档

    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('app_icon');
  final IOSInitializationSettings initializationSettingsIOS =
      IOSInitializationSettings(
    requestSoundPermission: false,
    requestBadgePermission: false,
    requestAlertPermission: false,
    onDidReceiveLocalNotification: onDidReceiveLocalNotification,
  );
  final MacOSInitializationSettings initializationSettingsMacOS =
      MacOSInitializationSettings(
          requestAlertPermission: false,
          requestBadgePermission: false,
          requestSoundPermission: false);
  final InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
      macOS: initializationSettingsMacOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: onSelectNotification);
  • 您可以传递 onSelectNotification 函数,每当用户单击通知时都会调用该函数

  • 从这个功能你可以重定向到特定的屏幕示例如下

           Future selectNotification(String payload) async {
             if (payload != null) {
               debugPrint('notification payload: $payload');
             }
             await Navigator.push(
                   context,
                   MaterialPageRoute<void>(builder: (context) => 
                   SecondScreen(payload)),);
           } 
    
于 2021-06-09T05:38:04.280 回答