我正在测试此this插件的本地通知。我想在按下按钮时显示通知,这是我的代码:
FlatButton(
onPressed: () {
FlutterLocalNotificationsPlugin localNotifPlugin =
new FlutterLocalNotificationsPlugin();
var androidChannelSpecifics = AndroidNotificationDetails(
'default',
'Test App Notifications',
'Channel for default notifications from test app');
var iOSChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidChannelSpecifics, iOSChannelSpecifics);
localNotifPlugin.show(
0,
"Test Notificaiton",
"This is your test notification! :)",
platformChannelSpecifics);
},
child: Text("Show Notification"),
)
这与该插件的文档非常相似,但无法正常工作,当我按下按钮时出现此错误:
D/EGL_emulation(24401): eglMakeCurrent: 0xa80053c0: ver 2 0 (tinfo 0xa8003360)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:156)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:131)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:598)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:690)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:619)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at android.os.Looper.loop(Looper.java:136)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at android.app.ActivityThread.main(ActivityThread.java:6077)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/MethodChannel#dexterous.com/flutter/local_notifications(24401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
E/flutter (24401): [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 (24401): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (24401): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (24401): <asynchronous suspension>
E/flutter (24401): #2 FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications.dart:120:20)
E/flutter (24401): <asynchronous suspension>
E/flutter (24401): #3 HomePage.build.<anonymous closure> (package:provider_test/main.dart:53:36)
E/flutter (24401): #4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter (24401): #5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter (24401): #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (24401): #7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter (24401): #8 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter (24401): #9 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter (24401): #10 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter (24401): #11 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter (24401): #12 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (24401): #13 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (24401): #14 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (24401): #15 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (24401): #16 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (24401): #17 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (24401): #18 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (24401): #19 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (24401): #20 _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter (24401): #21 _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
我做错了什么?
答案 0 :(得分:2)
我通过在初始化android设置时指示文件类型来解决问题,如下所示:
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon.png');
或者,您可以将图标设置为默认值,如下所示:
var initializationSettingsAndroid = AndroidInitializationSettings('mipmap/ic_launcher');
希望这会有所帮助。
答案 1 :(得分:0)
好像您正在初始化插件而没有或有错误的app_icon;
在您的initState中,您应使用正确的图标名称进行初始化:
@override
void initState() {
super.initState();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon'); // <- default icon name is @mipmap/ic_launcher
var initializationSettingsIOS = IOSInitializationSettings(onDidReceiveLocalNotification: onDidReceiveLocalNotification);
var initializationSettings = InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification); }
More examples here <-如何初始化
https://github.com/MaikuB/flutter_local_notifications#getting-started <-添加应用程序图标
答案 2 :(得分:0)
对于flutter local notifications,首先必须在android res / drawable文件夹中添加图标。然后,您应该在AndroidInitializationSettings类中提交此图标名称,否则会出错。
@override
void initState() {
super.initState();
var androidSettings = AndroidInitializationSettings(‘app_icon’);
var iOSSettings = IOSInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
);
var initSetttings = InitializationSettings(androidSettings, iOSSettings);
flutterLocalNotificationsPlugin.initialize(initSetttings, onSelectNotification: onClickNotification);
}