MaikuB / flutter_local_notifications

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

Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.content.Intent android.content.Intent.setAction(java.lang.String)' on a null object reference #1187

Closed dokumanx closed 3 years ago

dokumanx commented 3 years ago

Describe the bug When I catch the notification from FCM, after calling await _localNotification.show(0, title, body, _details); , I am getting this error.

E/flutter (12035): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.content.Intent android.content.Intent.setAction(java.lang.String)' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.setAction(java.lang.String)' on a null object reference
[        ] E/flutter (12035):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:165)
[        ] E/flutter (12035):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:791)
[        ] E/flutter (12035):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1054)
[        ] E/flutter (12035):   at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:947)
[        ] E/flutter (12035):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
[        ] E/flutter (12035):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
[        ] E/flutter (12035):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
[        ] E/flutter (12035):   at android.os.MessageQueue.nativePollOnce(Native Method)
[        ] E/flutter (12035):   at android.os.MessageQueue.next(MessageQueue.java:335)
[        ] E/flutter (12035):   at android.os.Looper.loop(Looper.java:183)
[        ] E/flutter (12035):   at android.app.ActivityThread.main(ActivityThread.java:7656)
[        ] E/flutter (12035):   at java.lang.reflect.Method.invoke(Native Method)
[        ] E/flutter (12035):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
[        ] E/flutter (12035):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
[        ] E/flutter (12035): )
[        ] E/flutter (12035): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:581:7)
[        ] E/flutter (12035): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
[        ] E/flutter (12035): <asynchronous suspension>
[        ] E/flutter (12035): #2      FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:179:7)
[        ] E/flutter (12035): <asynchronous suspension>
[        ] E/flutter (12035): #3      NotificationService._handleRemoteMessage (package:meh/services/notification_service.dart:66:9)
[        ] E/flutter (12035): <asynchronous suspension>
[        ] E/flutter (12035): 

To Reproduce

Run the code below .

Flutter sdk version: 2.0.5 Dart sdk version: ">=2.10.0 <3.0.0" flutter_local_notifications version: ^5.0.0+4

Expected behavior

It should display notification.

Sample code to reproduce the problem

Below code my notification service.

import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:meh/constants/constants.dart';

class NotificationService {
  final _messaging = FirebaseMessaging.instance;
  static final _localNotification = FlutterLocalNotificationsPlugin();

  void initialize() async {
    final _initializeAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    final _initializeIOS = IOSInitializationSettings();

    final _initializationSettings = InitializationSettings(
        android: _initializeAndroid, iOS: _initializeIOS);

    final result = await _localNotification.initialize(_initializationSettings);

    if (result) {
      if (Platform.isIOS) {
        _messaging.requestPermission();
      }

      //TODO: Handle token with createDevice mutation later.
      final token = await _messaging.getToken(vapidKey: vapidKey);

      final remoteMessage = await _messaging.getInitialMessage();

      _handleRemoteMessage(remoteMessage: remoteMessage);

      FirebaseMessaging.onMessage.listen(
        (remoteMessage) => _handleRemoteMessage(remoteMessage: remoteMessage),
      );

      FirebaseMessaging.onBackgroundMessage(
        (remoteMessage) async =>
            _handleRemoteMessage(remoteMessage: remoteMessage),
      );
    }
  }

  static Future<void> _handleRemoteMessage(
      {RemoteMessage remoteMessage}) async {
    if (remoteMessage != null) {
      final _androidDetails =
          AndroidNotificationDetails('Channel ID', 'Meh', 'Meh Channel');
      final _iosDetails = IOSNotificationDetails();
      final _macOsDetails = MacOSNotificationDetails();

      final _details = NotificationDetails(
          android: _androidDetails, iOS: _iosDetails, macOS: _macOsDetails);

      if (remoteMessage.data != null && remoteMessage.data.isNotEmpty) {
        print('############## REMOTE MESSAGE DATA ##############');
        print('${remoteMessage.data}');
      }

      if (remoteMessage.notification != null) {
        print('############## NOTIFICATION ##############');
        print('${remoteMessage.notification}');

        final title = remoteMessage.notification.title;
        final body = remoteMessage.notification.body;

        await _localNotification.show(0, title, body, _details);
      }
    }
  }
}
dokumanx commented 3 years ago

@MaikuB Hi. Can you help me with the error? I have been blocked for a while.

dokumanx commented 3 years ago

Package name comes null. It causes this error. Check the below PR out to see it in detail.

https://github.com/rafaelsetragni/awesome_notifications/pull/192

MaikuB commented 3 years ago

Do you have a link to a minimal app that can reproduce the error so that this would help verify that a fix works? Not that sure if that code snippet you have there is sufficient as I had an app to test integration with FCM but hadn't run into the error and haven't heard of others run into the same issue when using FCM as even the example app for the Flutter FCM makes use of this plugin, albeit an older version but should have the same code for getting the launch intent

dokumanx commented 3 years ago

Unfortunately, the above code is the only service that I used for notifications. And the error message is clear and shows the reason. For some reason, the package name is not visible in the plugin. And we had to check whether it is null. If it is null we give the name explicitly. I will try to use flutter_local_notification package one more time. And try to reproduce and fix this issue for anyone who deals with it. If you need more about the issue let me know.

MaikuB commented 3 years ago

As there are no steps to reproduce this, I'm going to have to close this for now. There was another similar issue opened at #1219 and it was solved. This turned out to be a configuration problem with another dependency. One of the official Flutter plugins also makes use of the same Android API to get the launch intent as well and I haven't seen issues reported on it either. One thing I want to point out is that the approach taken to solve the issue in the other notifications plugin that you showed may not work in the scenario where Flutter is added to an existing app

mukendi commented 3 years ago

Moi aussi j'ai la meme erreur

toco1001 commented 2 years ago

Same error. Have you solved this issue? @dokumanx

My env

elvisAR-git commented 2 years ago

same error :( Flutter version 2.10.4 was working fine and the suddenly stopped

mukendi commented 2 years ago

Mon problème étais au niveau du fichier manifest.xml

seungku commented 1 year ago

In my case, Intent-filter for deep link makes exception. After deleting the below code in AndroidManifest.xml, exception disappeared.

EloWeld commented 1 year ago

same error :(

DieGlueckswurst commented 1 year ago

same error, any solutions to this? @seungku I am not using deep links in my app.