【发布时间】: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<T>(String method, [dynamic arguments]) 中,我尝试打印method 和arguments 以查看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}
你可以看到title、body、channel id、channel NAME这些都是我给的,但还有很多其他字段是null。我猜这些会导致错误,但不确定。
当我按照文档进行操作时,我没有看到任何有关此类字段的说明。
一些线索将意味着很大的帮助。非常感谢您的宝贵时间。
【问题讨论】:
-
你解决了吗?
-
否,尚未解决。