我想在应用程序关闭时点击通知后导航到小部件(flutter_local_notifications 包)

问题描述 投票:0回答:1

我使用了

flutter_local_notifications
包,我想在应用程序关闭时点击通知后导航到小部件。

这是我的源代码:

import 'dart:async';
import 'dart:js';

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:second_project/Screens/PrivateUserChat.dart';

class NotificationManager {
  static FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;

  @pragma('vm:entry-point')
  static void notificationTapBackground(
      NotificationResponse notificationResponse) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return PrivateUserChatScreen(
        receiver_username: 'username',
      );
    }));
  }

  static Initialize() async {
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

    const initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');

    await flutterLocalNotificationsPlugin?.initialize(
      const InitializationSettings(
        android: initializationSettingsAndroid,
      ),
      onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
    );
  }

  static Future<void> ShowNotification(String msg, {String title = ""}) async {
    var androidChannelSpecifics = AndroidNotificationDetails(
      'CHANNEL_ID',
      'CHANNEL_NAME',
      //"CHANNEL_DESCRIPTION",
      importance: Importance.max,
      priority: Priority.high,
      playSound: true,
      timeoutAfter: 5000,
      styleInformation: DefaultStyleInformation(true, true),
    );
    var platformChannelSpecifics =
        NotificationDetails(android: androidChannelSpecifics);
    await flutterLocalNotificationsPlugin!.show(
      0, // Notification ID
      title, // Notification Title
      msg, // Notification Body, set as null to remove the body
      platformChannelSpecifics,
      payload: 'New Payload', // Notification Payload
    );
  }
}

但是我无法访问

context
中的
Navigator.push(context,...

请帮忙。

flutter dart flutter-local-notification
1个回答
0
投票

您无法从该隔离功能进行路由。当您的通知打开您的应用程序时,请检查您的

notificationTapBackground(NotificationResponse notificationResponse)函数。如果您的 notificationResponse 不为 null,那么您就可以继续下一步了。

因此,如果您的本地通知启动了您的应用程序,您可以调用以下 Future 函数从您喜欢的任何屏幕获取通知详细信息:

final NotificationAppLaunchDetails? notificationAppLaunchDetails =
        await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();

然后从通知中的详细信息路由到您所需的页面。显然,如果您在屏幕中执行上述功能,您应该有上下文。

© www.soinside.com 2019 - 2024. All rights reserved.