Search code examples
flutterflutter-local-notification

Flutter local notification sound works in most devices except Samsung A50 Android 11


I want to show Push notifications when the user open my flutter app . Notifications appear in most devices and have sound and appear like popup in Samsung and Realme but in my device Samsung A50, notifications appear but without sound and don't popup , just appear in notification bar . I created a raw folder in the res folder of Android project.

Here is my code

 import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:rxdart/rxdart.dart';

class NotificationApi{
  static final notifications = FlutterLocalNotificationsPlugin();
  static final onNotifications = BehaviorSubject<String?>();



  static Future notificationsDetails() async {
    var androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
      'Notification Channel ID',
      'Channel Name',
      importance: Importance.max,
      priority: Priority.high,
      sound: RawResourceAndroidNotificationSound('sound'),
     // ongoing: true,
    );

    var iOSPlatformChannelSpecifics = IOSNotificationDetails(
      //  sound: 'slow_spring_board.aiff'
    );

    return
    NotificationDetails(
        android: androidPlatformChannelSpecifics,
        iOS: iOSPlatformChannelSpecifics
    );

  }

  static Future init({bool initScheduled = false}) async {
    final android = AndroidInitializationSettings('@mipmap/ic_launcher');
    final ios = IOSInitializationSettings(
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,

    );
    final settings = InitializationSettings(android: android,iOS: ios);
    await notifications.initialize(
      settings,
    onSelectNotification: (payload) async {
        onNotifications.add(payload);
    });

  }

  static Future showNotification({int? id,
  String? title,
  String? body,
  String? payload}) async {
    notifications.show(id!, title, body, await notificationsDetails(),payload: payload);
  }
      

 }

In my home.dart

 @override
  void initState() {

    super.initState();

    NotificationApi.init();
    listenNotifications();
     /// some code
     showLocalNotification();
}


listenNotifications(){
    NotificationApi.onNotifications.stream.listen((event) {
      Navigator.of(context).push(MaterialPageRoute(builder: (context)=>InboxClass()));
    });
  }

showLocalNotification(List<OfferModel> offerList){

    for(var i=0;i<offerList.length;i++){

      NotificationApi.showNotification(title: offerList[i].NAME,body:offerList[i].DESCRIPTION,payload: "",id: i);
    }
  }

Solution

  • It could be that the mistake is in the importance parameter.

    importance: Importance.max

    should be

    importance: Importance.high

    Importance.max is not used by Android, see here