I'm testing out this this plugin for local notifications. I want to show a notification on a button press, this is my code:
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"),
)
This is very similar to the documentation for the plugin but its not working and I'm getting this error when I press the button:
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)
What did I do wrong?
I solve the problem by indicating the file type when initializing android setting as follows:
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon.png');
Alternatively, u can set the icon as default like this:
var initializationSettingsAndroid = AndroidInitializationSettings('mipmap/ic_launcher');
Hope this helps.
Looks like you are initializing plugin without or with wrong app_icon;
In your initState you should initialize with CORRECT ICON NAME:
@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); }
https://github.com/MaikuB/flutter_local_notifications#getting-started <- how to initialize
How to change the application launcher icon on Flutter? <- adding app icons
For flutter local notifications, first you must add icon in android res/drawable folder. Then you should submit this icon name in AndroidInitializationSettings class otherwise you will get error.
@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);
}
This is caused when there is error in initialization mainly because of app icon. Provide mipmap/ic_launcher or filename.png when initialising (filename.png is preferred). Otherwise you might have not called or wait for the initialization. Use await before calling onMessage listener.
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Message body: ${message.notification.body}');}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With