Flutter本地通知未在本机中显示

时间:2020-04-27 09:02:53

标签: flutter dart notifications

我在处理本地通知时遇到了麻烦。我使用flutter_local_notifications来显示本地通知。

我有一个Flutter应用程序,该应用程序已添加到本机Android应用程序中。通知可以在我运行flutter应用程序时出现,而在我使用本机Android运行flutter应用程序时则不会出现。

这是我使用的本地通知类。

class _NotificationKeys {
  static const channelId = "notification.channel.id";
  static const channelName = "notification.channel.name";
  static const channelDescription = "notification.channel.desc";
}

class LocalNotification {
  LocalNotification._privateConstructor() {
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    _flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    _flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: _onSelectNotification);
  }

  static final LocalNotification instance =
      LocalNotification._privateConstructor();

  FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin;

  List<Function(String payload)> _listeners = [];

  void addListener(Function(String payload) function) {
    _listeners.add(function);
  }

  Future _onSelectNotification(String payload) async {
    _listeners.forEach((function) {
      function(payload);
    });
  }

  Future showNotificationWithDefaultSound(
      int id, String title, String body, String payload) async {
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
        _NotificationKeys.channelId,
        _NotificationKeys.channelName,
        _NotificationKeys.channelDescription,
        importance: Importance.Max,
        priority: Priority.High);
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await _flutterLocalNotificationsPlugin.show(
      id,
      title,
      body,
      platformChannelSpecifics,
      payload: payload,
    );
  }
}

这是我在调用_flutterLocalNotificationsPlugin.show之后遇到的错误。

Attempt to invoke virtual method 'java.lang.String java.lang.Class.getName()' on a null object reference

这是堆栈跟踪。

StandardMethodCodec.decodeEnvelope (message_codecs.dart:569)
MethodChannel._invokeMethod (platform_channel.dart:156)
<asynchronous gap>
MethodChannel.invokeMethod (platform_channel.dart:329)
AndroidFlutterLocalNotificationsPlugin.show (platform_flutter_local_notifications.dart:137)
FlutterLocalNotificationsPlugin.show (flutter_local_notifications_plugin.dart:136)
LocalNotification.showNotificationWithDefaultSound (local_notification.dart:49)

我在flutter_local_notifications存储库中搜索了此问题,但未找到任何结果。如果您找到解决此问题的方法,请赐教。谢谢。

0 个答案:

没有答案