如何解决 flutter_local_notifications PlatformException 错误?

Rat*_*rty 10 android ios dart flutter

flutter_local_notifications第一次在项目中使用包。

遵循pub.dev站点文档中给出的代码。但问题是当我按下应该触发通知的按钮时,它会导致PlatformException错误。

错误

E/flutter (15180): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null)
E/flutter (15180): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (15180): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:318:33)
E/flutter (15180): <asynchronous suspension>
E/flutter (15180): #2      FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications.dart:120:20)
E/flutter (15180): <asynchronous suspension>
E/flutter (15180): #3      _NewsfeedState.showNotification (package:news_app/screens/newsfeed_screen.dart:129:43)

Run Code Online (Sandbox Code Playgroud)

代码如下。

初始化状态()

  @override
  void initState() {
   super.initState();

    var android = AndroidInitializationSettings('mipmap-hdpi/ic_launcher.png');
    var ios = IOSInitializationSettings();
    var initSettings = InitializationSettings(android, ios);
    flutterLocalNotificationsPlugin.initialize(initSettings, onSelectNotification: onSelectNotification);

}
Run Code Online (Sandbox Code Playgroud)

onSelectNotification():按下通知时显示一个对话框。

Future onSelectNotification(String payload) {
    showDialog(context: context, builder: (_) => AlertDialog(
      title: Text("Notfication"),
      content: Text("This is notification"),
    ));
  }

Run Code Online (Sandbox Code Playgroud)

主要小部件Press作为测试目的,我使用此按钮检查通知是否有效。


  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        RaisedButton(
          onPressed: showNotification,
          child: Text(
              "Press"
          ),
        ),
      ],
    );
  }

Run Code Online (Sandbox Code Playgroud)

显示通知()

showNotification() async {
    var android = AndroidNotificationDetails(
        'channel id',
        'channel NAME',
        'CHANNEL DESCRIPTION',
        importance: Importance.Max,
        priority: Priority.High,
        ticker: 'ticker'
    );
    var ios = IOSNotificationDetails();
    var platform = NotificationDetails(android, ios);
    await flutterLocalNotificationsPlugin.show(0, "New Notification", "I'm notification", 
    platform, payload: "It works");
  }

Run Code Online (Sandbox Code Playgroud)

还有一个问题,我发现这个错误,但它提供了解决方案是没有用的,我的。

我尝试flutter_local_notifications了几次更改软件包的版本,但没有成功。

然后追溯错误中显示的文件,以查看究竟是什么导致了null值。

在文件中platform_channel.dartinvokeMethod<T>(String method, [dynamic arguments])我尝试打印methodarguments查看结果是什么null。这个方法是从 调用的flutter_local_notifications.dart

invokeMethod(String method, [动态参数])

 @optionalTypeArgs
  Future<T> invokeMethod<T>(String method, [ dynamic arguments ]) async {
    assert(method != null);
    final ByteData result = await binaryMessenger.send(
      name,
      codec.encodeMethodCall(MethodCall(method, arguments)),
    );

    print(arguments);  
    print(method); 

    if(result == null) {
      throw MissingPluginException('No implementation found for method $method on channel 
      $name');
    }
    final T typedResult = codec.decodeEnvelope(result);
    return typedResult;
  }

Run Code Online (Sandbox Code Playgroud)

参数的输出是,

I/flutter (15180): {id: 0, title: New News!, body: I'm news., platformSpecifics: {icon: null, channelId: channel id, channelName: channel NAME, channelDescription: CHANNEL DESCRIPTION, channelShowBadge: true, channelAction: 0, importance: 5, priority: 1, playSound: true, sound: null, enableVibration: true, vibrationPattern: null, style: 0, styleInformation: {htmlFormatContent: false, htmlFormatTitle: false}, groupKey: null, setAsGroupSummary: null, groupAlertBehavior: 0, autoCancel: true, ongoing: null, colorAlpha: null, colorRed: null, colorGreen: null, colorBlue: null, largeIcon: null, largeIconBitmapSource: null, onlyAlertOnce: null, showProgress: false, maxProgress: 0, progress: 0, indeterminate: false, enableLights: false, ledColorAlpha: null, ledColorRed: null, ledColorGreen: null, ledColorBlue: null, ledOnMs: null, ledOffMs: null, ticker: ticker}, payload: I'm the one}

你可以看到titlebodychannel idchannel NAME这些都是我给的人,但也有许多其他字段其null。我猜这些会导致错误,但不确定。

当我按照文档进行操作时,我没有看到有关此类字段的任何说明。

一些线索将意味着很大的帮助。非常感谢您的时间。

Ali*_*ssa 3

来自插件的自述文件

app_icon 需要作为可绘制资源添加到 Android head 项目中

因此,将您的放入app_icon文件drawable夹而不是mipmap,并将代码更改为:

var android = AndroidInitializationSettings('app_icon');
Run Code Online (Sandbox Code Playgroud)