I'm using flutter_local_notifications
package in a project for the first time.
Followed the codes given in the documentation in pub.dev
site. But the problem is when I press the button that should trigger the notification it is causing a PlatformException
error.
The Error
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)
The codes are given below.
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() : It shows a Dialog box when the notification is pressed.
Future onSelectNotification(String payload) {
showDialog(context: context, builder: (_) => AlertDialog(
title: Text("Notfication"),
content: Text("This is notification"),
));
}
Main widget : I was using this Press
button to check if the notification works, as a test purpose.
@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");
}
There is a question that I found about this error, but the solution it provides was of no use for me.
I tried changing the version of the flutter_local_notifications
package couple of times, did not work.
Then traced back the files that were shown in the error to see what exactly is causing null
value.
In the file platform_channel.dart
, inside the invokeMethod<T>(String method, [dynamic arguments])
I tried to print the method
and arguments
to see what is coming out to be null
. This method is called from flutter_local_notifications.dart
.
invokeMethod(String method, [ dynamic arguments ])
@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;
}
The output of arguments is,
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}
You can see the title
, body
, channel id
, channel NAME
these are all the ones I gave but there are many other fields which are null
. I'm guessing these to cause the error, but not certain.
As I followed the documentation, I did not see any instruction for such fields.
Some clue would mean great help. Thanks a lot for your time.
From plugin's readme:
app_icon needs to be a added as a drawable resource to the Android head project
So put your app_icon
into drawable
folder instead of mipmap
, and change your code to:
var android = AndroidInitializationSettings('app_icon');
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