我的Flutter本地通知有一个错误

时间:2019-06-28 12:38:15

标签: android flutter notifications

我需要帮助,致电本地通知时出错。

对于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(错误,尝试在空对象引用上调用虚拟方法'int java.lang.Integer.intValue()',null))”

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

6 个答案:

答案 0 :(得分:7)

我已经遇到了这个问题,就我而言,这是一个图标问题app_icon
在您的initState函数中替换此

 var initializationSettingsAndroid = new AndroidInitializationSettings('app_icon');

与此

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

希望这对您有所帮助。

答案 1 :(得分:1)

在我的情况下,我在以下路径中将app_icon.png添加到a​​ndroid drawable中: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> 

答案 2 :(得分:0)

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

  @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);
    }
}

答案 3 :(得分:0)

您的插件初始化有问题。我在您的代码中看到,您创建了实例 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 :(得分:0)

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

var androidSettings = AndroidInitializationSettings('app_icon');

答案 5 :(得分:0)

将您的 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');