【问题标题】:How to solve flutter_local_notifications PlatformException error?如何解决flutter_local_notifications PlatformException错误?
【发布时间】:2020-06-01 14:35:15
【问题描述】:

我是第一次在项目中使用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)

代码如下。

initState()

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

}

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

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

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


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

showNotification()

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

我发现了一个关于此错误的question,但它提供的解决方案对我没有用。

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

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

在文件platform_channel.dart 中,在invokeMethod&lt;T&gt;(String method, [dynamic arguments]) 中,我尝试打印methodarguments 以查看null 的结果。这个方法是从flutter_local_notifications.dart调用的。

invokeMethod(字符串方法,[动态参数])

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

参数的输出是,

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。我猜这些会导致错误,但不确定。

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

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

【问题讨论】:

  • 你解决了吗?
  • 否,尚未解决。

标签: android ios flutter dart


【解决方案1】:

在安卓项目中。在文件夹“res”下,文件夹“mipmap/launcher_icon”中有一个图标,如图所示。 Folder structue

为了使用它,你可以设置AndroidNotificationDetails

 AndroidNotificationDetails(
    'id', 'your channel name', 'your channel description',
    importance: Importance.max,
    priority: Priority.high,
    showWhen: false,
icon: "mipmap/launcher_icon"); //the important part

所以只需设置图标:"mipmap/launcher_icon",因为那是 android 资源名称。

【讨论】:

    【解决方案2】:

    我在这篇文章中找到了适合我的解决方案:

    https://stackoverflow.com/a/60236675/11192570

    使用此代码:

      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
              new FlutterLocalNotificationsPlugin();
          var androidPlatformChannelSpecifics = AndroidNotificationDetails(
              '1687497218170948721x8',
              'New Trips Notification ',
              'Notification Channel for vendor. All the new trips notifications will arrive here.',
              style: AndroidNotificationStyle.BigText,
              icon: "app_icon",
              styleInformation: BigTextStyleInformation(
                "hanji hogya?",//  'Locations: <b>${locations.replaceAll("\$", " to ")}</b><br>Vehicle: <b>$vehicle</b><br>Trip Type: <b>$tripType</b><br>Pick-Up Date: <b>$pickUpDate</b><br>Pick-Up Time: <b>$pickUpTime</b>',
                  htmlFormatBigText: true,
                  contentTitle: 'Amount:- <b>Rs 22000000</b>',
                  htmlFormatContentTitle: true,
                  summaryText: 'Trip Details',
                  htmlFormatSummaryText: true));
    

    【讨论】:

      【解决方案3】:

      来自插件的readme

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

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

      var android = AndroidInitializationSettings('app_icon');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-16
        • 2021-12-29
        • 2018-06-30
        • 2012-02-18
        • 2011-08-30
        • 2014-06-13
        相关资源
        最近更新 更多