Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There's some error on my Flutter Local Notification

I need help, there's an error when calling Local Notification.

For the initState :

initState() {
    super.initState();
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    FlutterLocalNotificationsPlugin().initialize(initializationSettings, onSelectNotification: onSelectNotification);
  }

For the function :

showNotification() async {
    var android = new AndroidNotificationDetails('Channel ID', 'Channel Name', 'channelDescription');
    var iOS = new IOSNotificationDetails();
    var platform = new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
      0, 'New Notification', 'Flutter Local Notif', platform,payload: 'test notification');
  }

The error is "PlatformException (PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null))"

I've already tried on the documentation and also youtube, but I always get this error message

like image 636
Jeff Avatar asked Jun 28 '19 12:06

Jeff


People also ask

How do I Flutter Flutter local notifications?

Displaying a notification in Flutter To display a notification, we need to create a platform-specific NotificationDetails instance, which takes in arguments that are unique to each platform. AndroidNotificationDetails handles the configuration of notifications in Android devices.

How do I display images in Flutter local notification?

To achieve an image on local notification, first add the awesome_notifications Flutter package by adding the following lines in pubspec. yaml file. awesome_notifications is the best flutter package we got to show local notifications in the Flutter app.

How do I add notifications on Flutter?

In the main app target, select Signing & Capabilities > All > + Capability and then search "push." Double-click on Push Notifications to enable it. Next, enable Background Modes and check Remote Notifications. Now you are ready to send notifications from the OneSignal dashboard!


2 Answers

I have faced this, and in my case it was an icon problem app_icon
in your initState function replace this

 var initializationSettingsAndroid = new AndroidInitializationSettings('app_icon');

with this

 var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');

Hope this helps you.

like image 169
SlimenTN Avatar answered Oct 05 '22 10:10

SlimenTN


Add your app_icon.png to MY_PROJECT\android\app\src\main\res\drawable\app_icon.png

However, for me the error was in androidInitialization. I was initializing it this way:

var androidInit = AndroidInitializationSettings('app_icon.png');

Here, the .png shouldn't be added. I know this might be silly but if it helps anyone out there facing the same problem then there you go! This is the correct method:

var androidInit = AndroidInitializationSettings('app_icon');
like image 31
Ali Solanki Avatar answered Oct 05 '22 10:10

Ali Solanki