12

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.

2
  • Did you solve this? Commented Apr 18, 2020 at 3:06
  • 1
    No, yet to solve. Commented Apr 19, 2020 at 10:48

3 Answers 3

3

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');
Sign up to request clarification or add additional context in comments.

Comments

2

In android project. Under the folder "res" you have an icon in the folder "mipmap/launcher_icon" like it is shown in this picture. Folder structue

In order to use it, you can set up the AndroidNotificationDetails

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

So just set the icon: "mipmap/launcher_icon" since that is the android resource name.

Comments

0

I have found the solution for me in this post:

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

with this code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.