Home > database >  Flutter local notification not working here is my error
Flutter local notification not working here is my error

Time:08-30

Hello i just added flutter local notifications package to my project, I watch a video tutorial step by step but when I press the button nothing appear, no push notification here is my error message:

Hello i just added flutter local notifications package to my project, I watch a video tutorial step by step but when I press the button nothing appear, no push notification here is my error message:

Hello i just added flutter local notifications package to my project, I watch a video tutorial step by step but when I press the button nothing appear, no push notification here is my error message:

StackTrace:

E/flutter (13614): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (13614): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177:18)
E/flutter (13614): <asynchronous suspension>
E/flutter (13614): #2      FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:215:7)
E/flutter (13614): <asynchronous suspension>
E/flutter (13614): #3      LocalNotificationService.showNotification (package:electromobility_flutter_application/services/local_notification_service.dart:56:5)
E/flutter (13614): <asynchronous suspension>
E/flutter (13614): #4      _WalletState.build.<anonymous closure> (package:electromobility_flutter_application/Account/Wallet/WalletPage.dart:309:31)
E/flutter (13614): <asynchronous suspension>
E/flutter (13614):

Full code:

    class LocalNotificationService {
  LocalNotificationService();

  final _localNotificationService = FlutterLocalNotificationsPlugin();
  
  Future<void> initialize() async{
    const AndroidInitializationSettings androidInitializationSettings=
        AndroidInitializationSettings('@drawable/ic_stat_android');

     IOSInitializationSettings iosInitializationSettings = IOSInitializationSettings(
      requestAlertPermission: true,
      requestBadgePermission: true,
      requestSoundPermission: true,
      onDidReceiveLocalNotification: _onDidReceiveLocalNotification
    );


     final InitializationSettings settings = InitializationSettings(
       android: androidInitializationSettings,
       iOS: iosInitializationSettings,
     );
     await _localNotificationService.initialize(
       settings,
       onSelectNotification: onSelectNotification,
     );
  }

  Future<NotificationDetails> _notificationsDetails() async {
    const AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails('channel_id', 'channel_name',
        channelDescription: 'description',
          importance: Importance.max,
          priority: Priority.max,
          playSound: true);

    const IOSNotificationDetails iosNotificationDetails =
        IOSNotificationDetails();
    return const NotificationDetails(
      android: androidNotificationDetails,
      iOS: iosNotificationDetails,
    );

  }

  Future<void> showNotification({
  required int id,
    required String title,
    required String body,
}) async {
    final details = await _notificationsDetails();
    await _localNotificationService.show(id, title, body, details);
  }

  void _onDidReceiveLocalNotification(
      int id,String? title,String? body,String? payload) {
    print('id $id');
  }

  void onSelectNotification(
      String? payload) {
    print('payload $payload');
  }

}

screen 2:

onPressed: () async {await service.showNotification(id: 0, title: 'title', body: 'body');

CodePudding user response:

It may be because of wrong(unextisted) notification icon. Check it and maybe use it from another folder.

I think you already added this line to your code, but I have to mention(Use it when the app starts):

WidgetsFlutterBinding.ensureInitialized();

Also, have you added this to your 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> 
  • Related