MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.47k stars 1.4k forks source link

Help: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' #2325

Closed nissaba closed 5 months ago

nissaba commented 6 months ago

I have been able to get this working on the iOS, but Android his giving me the folowing error int he console and the notification is never shown.

E/MethodChannel#dexterous.com/flutter/local_notifications(14959): Failed to handle method call
E/MethodChannel#dexterous.com/flutter/local_notifications(14959): 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(14959):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:463)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:364)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:1248)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1588)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:1413)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at android.os.Handler.handleCallback(Handler.java:959)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at android.os.Handler.dispatchMessage(Handler.java:100)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at android.os.Looper.loopOnce(Looper.java:232)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at android.os.Looper.loop(Looper.java:317)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at android.app.ActivityThread.main(ActivityThread.java:8501)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/MethodChannel#dexterous.com/flutter/local_notifications(14959):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
E/flutter (14959): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] 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 (14959):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:463)
E/flutter (14959):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:364)
E/flutter (14959):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:1248)
E/flutter (14959):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1588)
E/flutter (14959):  at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:1413)
E/flutter (14959):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/flutter (14959):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/flutter (14959):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/flutter (14959):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (14959):  at android.os.Handler.handleCallback(Handler.java:959)
E/flutter (14959):  at android.os.Handler.dispatchMessage(Handler.java:100)
E/flutter (14959):  at android.os.Looper.loopOnce(Looper.java:232)
E/flutter (14959):  at android.os.Looper.loop(Looper.java:317)
E/flutter (14959):  at android.app.ActivityThread.main(ActivityThread.java:8501)
E/flutter (14959):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (14959):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/flutter (14959):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
E/flutter (14959): )
E/flutter (14959): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
E/flutter (14959): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
E/flutter (14959): <asynchronous suspension>
E/flutter (14959): #2      FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:240:7)
E/flutter (14959): <asynchronous suspension>
E/flutter (14959): 
mobiledev@ mobiledev-MacBook-Pro myapp_flutter % flutter doctor -v
[✓] Flutter (Channel stable, 3.22.0, on macOS 14.4.1 23E224 darwin-arm64, locale en-CA)
    • Flutter version 3.22.0 on channel stable at /Users/mobiledev/flutter_dev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (7 days ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/mobiledev/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • Pixel 6 (mobile)                • ********             • android-arm64  • Android 14 (API 34)
    • iPhone (mobile)                 • ******-******* • ios            • iOS 17.4.1 21E236

[✓] Network resources
    • All expected network resources are available.

• No issues found!

I am using environment: sdk: '>=3.3.4 <4.0.0'

I am uncertain what part of the code would trigger this. Could possibly not using a mipmap icon be the problem?

this is my helper class

class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin =
      FlutterLocalNotificationsPlugin();

  Future<void> initNotification() async {
    AndroidInitializationSettings initializationSettingsAndroid =
        const AndroidInitializationSettings('app_icons/egrid_app_icon.png');

    var initializationSettingsIOS = DarwinInitializationSettings(
        requestAlertPermission: true,
        requestBadgePermission: true,
        requestSoundPermission: true,
        onDidReceiveLocalNotification:
            (int id, String? title, String? body, String? payload) async {});
    var initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    await notificationsPlugin.initialize(initializationSettings,
        onDidReceiveNotificationResponse:
            (NotificationResponse notificationResponse) async {});
  }

  Future showNotification(
      {int id = 100, String? title, String? body, String? payload}) async {
    return notificationsPlugin.show(
        id, title, body, await notificationDetails());
  }

  notificationDetails() {
    return const NotificationDetails(
        android: AndroidNotificationDetails('argonChanel', 'argonNotifications',
            importance: Importance.max),
        iOS: DarwinNotificationDetails());
  }
}
sadhan46 commented 5 months ago

The issue is most probably due to not adding the mipmap icon.You can find it in Androidmanifest.xml "android:icon" for testing

nissaba commented 5 months ago

thanks. I was hoping to be able to use normal image assets, but it is not possible.