Jeff
Jeff

Reputation: 191

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

Upvotes: 13

Views: 27622

Answers (7)

user20449431
user20449431

Reputation: 1

uture _requestPermissions() async { const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('app_icon');

/// Note: permissions aren't requested here just to demonstrate that can be
/// done later
final DarwinInitializationSettings initializationSettingsIOS =
    DarwinInitializationSettings(
        requestAlertPermission: false,
        requestBadgePermission: false,
        requestSoundPermission: false,
        onDidReceiveLocalNotification: (
          int id,
          String? title,
          String? body,
          String? payload,
        ) async {
          didReceiveLocalNotificationSubject.add(
            ReceivedNotification(
              id: id,
              title: title,
              body: body,
              payload: payload,
            ),
          );
        });

Upvotes: 0

Szynkie
Szynkie

Reputation: 79

for me adding WidgetsFlutterBinding.ensureInitialized(); to main.dart before creating AndroidInitializationSettings helps.

Upvotes: 0

Ali Solanki YouTuber
Ali Solanki YouTuber

Reputation: 1488

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

Upvotes: 3

Islam Ahmed
Islam Ahmed

Reputation: 736

In my case, I added app_icon.png to the android drawables, in this path : MY_PROJECT\android\app\src\main\res\drawable

Take a look at the source code here https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications/example/android/app/src/main/res/drawable

Copy Paste all drawables to your project, it will work.

And Don't Forget to add these receivers to Manifest

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
    <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
        </intent-filter>
    </receiver> 

Upvotes: 1

iAkshay
iAkshay

Reputation: 1263

There is problem in your plugin initialization. I see in your code, you created the instance flutterLocalNotificationsPlugin but used FlutterLocalNotificationsPlugin().initialize() instead. And then you tried to show notification with the created instance which has not been initialized.

I got this error for the same reason - my FlutterLocalNotificationsPlugin was not initialized properly. To check if it is, I tried the below code:

void main() {
  runApp(MyApp());
  initializeNotification(); //Its Important to place this line after runApp() otherwise FlutterLocalNotificationsPlugin will not be initialize and you will get the error as mentioned in the question.
}


void initializeNotification() async {
  try {
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = IOSInitializationSettings();
    var initializationSettings = InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
    await flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: didSelectNotification);
  } catch(e) {
   print(e.toString());
  }
}

If it catch any exception, your flutterLocalNotificationsPlugin has not been initialized, and you will get an error.

Also try to wrap initialization code in separate async-await function.

Upvotes: 1

SlimenTN
SlimenTN

Reputation: 3564

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.

Upvotes: 23

Murat Aslan
Murat Aslan

Reputation: 1580

this one works for me you can give a chance

  @override
  initState() {
    super.initState();
to the Android head project
    var initializationSettingsAndroid =
    AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  }



  Future<void> _showNotification() async {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your channel id', 'your channel name', 'your channel description',
        importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
    var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(
        0, 'plain title', 'plain body', platformChannelSpecifics,
        payload: 'item x');
  }



 Future<void> onSelectNotification(String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
}

Upvotes: 1

Related Questions