1

I'm trying to send a notification with flutter on android using this dependency:

"flutter_local_notifications: ^9.0.1"

But it gives me this error when I click the button "test".

Anyone has an idea how can I solve it?

////////////////////////////////////////////////////////////////////////

In main.dart:

 ElevatedButton(
      onPressed: () => NotificationApi.showNotification(
          title: 'Samira', body: 'Heyy theree !!!', payload: 'samira'),
      child: Text("test"),

In notification.dart:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

class NotificationApi {
  static final _notifications = FlutterLocalNotificationsPlugin();

  static Future _notificationDetails() async {
    return const NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        channelDescription: 'channel description',
        importance: Importance.max,
      ),
      //iOS: IOSNotificationDetails(),
    );
  }

  static Future showNotification({
    int id = 0,
    String? title,
    String? body,
    String? payload,
  }) async =>
      _notifications.show(
        id,
        title,
        body,
        await _notificationDetails(),
        payload: payload,
      );
}


The error in my console:

Performing hot reload...
Syncing files to device Android SDK built for x86...
Reloaded 1 of 1023 libraries in 426ms.
E/MethodChannel#dexterous.com/flutter/local_notifications(15565): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications(15565): 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(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:238)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:180)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:793)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1056)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:949)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:872)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.os.Looper.loop(Looper.java:136)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at android.app.ActivityThread.main(ActivityThread.java:6077)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/MethodChannel#dexterous.com/flutter/local_notifications(15565):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
E/flutter (15565): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:238)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:180)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:793)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1056)
E/flutter (15565):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:949)
E/flutter (15565):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (15565):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:84)
E/flutter (15565):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:872)
E/flutter (15565):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (15565):  at android.os.MessageQueue.next(MessageQueue.java:323)
E/flutter (15565):  at android.os.Looper.loop(Looper.java:136)
E/flutter (15565):  at android.app.ActivityThread.main(ActivityThread.java:6077)
E/flutter (15565):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (15565):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/flutter (15565):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
E/flutter (15565): )
E/flutter (15565): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (15565): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter (15565): <asynchronous suspension>
E/flutter (15565): #2      FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:194:7)
E/flutter (15565): <asynchronous suspension>
E/flutter (15565): 

1 Answer 1

6

You should add the notification icon properly. It solved my issue.

  static Future _notificationDetails() async {
    return const NotificationDetails(
      android: AndroidNotificationDetails(
        'channel id',
        'channel name',
        channelDescription: 'channel description',
        importance: Importance.max,
        icon: "ic_notification", //<-- Add this parameter
        ),
      //iOS: IOSNotificationDetails(),
    );
  }
Sign up to request clarification or add additional context in comments.

1 Comment

It work thank you, do you have any idea who can I make it work in background even if my app is closed? (like whats-app, or any other chat app)

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.