【问题标题】:There's some error on my Flutter Local Notification我的 Flutter 本地通知有一些错误
【发布时间】:2019-06-28 12:38:15
【问题描述】:

我需要帮助,调用本地通知时出错。

对于 initState :

initState() {
    super.initState();
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    FlutterLocalNotificationsPlugin().initialize(initializationSettings, onSelectNotification: onSelectNotification);
  }

对于功能:

showNotification() async {
    var android = new AndroidNotificationDetails('Channel ID', 'Channel Name', 'channelDescription');
    var iOS = new IOSNotificationDetails();
    var platform = new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
      0, 'New Notification', 'Flutter Local Notif', platform,payload: 'test notification');
  }

错误是“PlatformException (PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on an null object reference, null))”

我已经尝试过文档和 youtube,但我总是收到此错误消息

【问题讨论】:

  • 所以它在“await flutterLocalNotificationsPlugin.show(0, 'New Notification', 'Flutter Local Notif', platform,payload: 'test notification');”上崩溃?使用调试器和断点可以吗?看看什么值返回 null?
  • 所以当我尝试使用断点时,didReceiveLocalNotificationCallback 为空,而在 _platform 目录中,可执行文件为空,executableArguments:List (0 items)

标签: android flutter notifications


【解决方案1】:

我遇到过这个问题,就我而言,这是一个图标问题app_icon
在你的 initState 函数中替换这个

 var initializationSettingsAndroid = new AndroidInitializationSettings('app_icon');

有了这个

 var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');

希望对你有所帮助。

【讨论】:

  • 这解决了我的问题,谢谢!
【解决方案2】:

这个对我有用,你可以给个机会

  @override
  initState() {
    super.initState();
to the Android head project
    var initializationSettingsAndroid =
    AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  }



  Future<void> _showNotification() async {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your channel id', 'your channel name', 'your channel description',
        importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
    var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
        0, 'plain title', 'plain body', platformChannelSpecifics,
        payload: 'item x');
  }



 Future<void> onSelectNotification(String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
}

【讨论】:

  • 使用此代码时出现错误。在构建 Builder 时引发了以下 NoSuchMethodError。在 null.Receiver:null 上调用了方法“initialize”。尝试调用:初始化('InitializationSettings'的实例,onSelectNotification:关闭:(字符串)=> Future from Function 'onSelectNotifications':.)
  • 所以在我清理并重建我的应用程序后,有一个新的错误语句。 E/flutter ( 8430): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'show' was called on null. E/flutter ( 8430): Receiver: null E/flutter ( 8430): Tried calling: show(0, "plain title", "plain body", Instance of 'NotificationDetails', payload: "item x")
【解决方案3】:

您的插件初始化有问题。我在您的代码中看到,您创建了实例 flutterLocalNotificationsPlugin 但使用 FlutterLocalNotificationsPlugin().initialize() 代替。然后您尝试使用尚未初始化的已创建实例显示通知。

出于同样的原因,我收到此错误 - 我的 FlutterLocalNotificationsPlugin 未正确初始化。为了检查它是否是,我尝试了以下代码:

void main() {
  runApp(MyApp());
  initializeNotification(); //Its Important to place this line after runApp() otherwise FlutterLocalNotificationsPlugin will not be initialize and you will get the error as mentioned in the question.
}


void initializeNotification() async {
  try {
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = IOSInitializationSettings();
    var initializationSettings = InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
    await flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: didSelectNotification);
  } catch(e) {
   print(e.toString());
  }
}

如果它捕捉到任何异常,说明你的flutterLocalNotificationsPlugin还没有初始化,你会得到一个错误。

还尝试将初始化代码包装在单独的 async-await 函数中。

【讨论】:

    【解决方案4】:

    在我的例子中,我将app_icon.png 添加到 android drawables 中,在这个路径中:MY_PROJECT\android\app\src\main\res\drawable

    在此处查看源代码 https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications/example/android/app/src/main/res/drawable

    将所有可绘制对象复制粘贴到您的项目中,它会起作用。

    并且不要忘记将这些接收者添加到清单中

    <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
            </intent-filter>
        </receiver> 
    

    【讨论】:

      【解决方案5】:

      将您的app_icon.png 添加到MY_PROJECT\android\app\src\main\res\drawable\app_icon.png

      但是,对我来说,错误在于 androidInitialization。我是这样初始化的:

      var androidInit = AndroidInitializationSettings('app_icon.png');
      

      此处不应添加.png。我知道这可能很愚蠢,但如果它可以帮助任何面临同样问题的人,那么你就去吧!这是正确的方法:

      var androidInit = AndroidInitializationSettings('app_icon');
      

      【讨论】:

      • 谢谢。这并不傻。即使我遇到了同样的问题
      【解决方案6】:

      要在android平台初始化本地通知设置,您必须指定通知图标名称,否则通知将不会在android平台中显示。如果您只是在 AndroidInitializationSettings 类中给出图像名称,则通知图标必须放在 android res/drawable 文件夹下。您也可以查看flutter local notifications example 以获取更多参考。

      var androidSettings = AndroidInitializationSettings('app_icon');
      

      【讨论】:

        【解决方案7】:

        对我来说,在创建 AndroidInitializationSettings 之前将 WidgetsFlutterBinding.ensureInitialized(); 添加到 main.dart 会有帮助。

        【讨论】:

          猜你喜欢
          • 2020-12-03
          • 2022-11-29
          • 2013-12-09
          • 1970-01-01
          • 1970-01-01
          • 2022-01-21
          • 1970-01-01
          • 2020-06-09
          • 2021-03-17
          相关资源
          最近更新 更多