2

The notification sound using Flutter Local Notification for Android not working at all here is the code

const NotificationDetails(
          android: AndroidNotificationDetails(
            'daily notification channel id',
            'daily notification channel name',
            'daily notification description',
            playSound: true,
            sound: RawResourceAndroidNotificationSound('pop'),
            importance: Importance.max,
            priority: Priority.high,
          ),
        ),

And I have pop.mp3 in this path: \android\app\src\main\res\raw\pop.mp3

How can I play the sound?

Ammar Mohamed
  • 513
  • 1
  • 6
  • 16

5 Answers5

4

My problem was that I was testing the app on Xiaomi and I realized that Xiaomi has some problems and it by default doesn't allow notification sound. Somehow it didn't work on the emulator too, but by testing it on other physical devices it worked.

Ammar Mohamed
  • 513
  • 1
  • 6
  • 16
  • I have been getting play store comments saying the notification sounds aren’t working on some devices, I’m thinking it could be the Xiaomi devices, did you find a fix? – Chris Jan 09 '22 at 11:23
  • I agree with you. I was facing the same problem on my Xiaomi phone and the notification sound was working fine in the emulator but not working on my phone, in the end, I discovered that my Xiaomi phone turned off the notification sound so I allowed it and it worked fine. You have to go to app settings then notification settings then allow notification sound and it will work fine. – Mostafa Alazhariy Apr 07 '22 at 00:35
3

For Android 8.0+, sounds and vibrations are associated with notification channels and can only be configured when they are first created. Showing/scheduling a notification will create a channel with the specified id if it doesn't exist already. If another notification specifies the same channel id but tries to specify another sound or vibration pattern then nothing occurs.

Solution => Please change Channel id, name, description, it will be fine.

  • This answer saved me from lots of confusion! – sm_sayedi Nov 04 '22 at 19:53
  • This answer was way to go, after doing this it started working, also i have a question, is the Keep.xml file necessary inside raw folder as i saw that in another stackoverflow answer? – Arham S C Jul 15 '23 at 15:21
0

you can play asset sound, This worked 100% in my case

The property below:

sound: const UriAndroidNotificationSound("assets/tunes/pop.mp3"),

await flutterLocalNotificationsPlugin.schedule(
    id,
    "notification",
    'Notification Alert',
    dateTime,
    NotificationDetails(
      android: AndroidNotificationDetails(
          "channel.id",
          "channel.name",
          importance: Importance.max,
          priority: Priority.high,
          color: primaryColor,
          playSound: true,
          sound:  const UriAndroidNotificationSound("assets/tunes/pop.mp3"),
          icon: '@mipmap/ic_launcher'
      )
  )

);
umer qazi
  • 9
  • 2
-1

add this code in Manifest.xml

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
....
</application>
Ary Anzh
  • 406
  • 3
  • 15
-1

I've had a problem like yours before, but it's finished by applying the code below

class AddTaskScreen extends StatefulWidget {
  static String id = 'AddTaskScreen';

  @override
  _AddTaskScreenState createState() => _AddTaskScreenState();
}

class _AddTaskScreenState extends State<AddTaskScreen> {
  final taskController = TextEditingController();
  final deskController = TextEditingController();

  String currTask = '';
  String deskTask = '';
  bool remindMe = false;
  DateTime reminderDate;
  TimeOfDay reminderTime;
  int id;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Text(
              Kunci.add_task.tr(),
              textAlign: TextAlign.center,
              style: TextStyle(
                fontWeight: FontWeight.w500,
              ),
            ),
            SizedBox(height: 15),
            TextFormField(
              controller: taskController,
              autofocus: false,
              textCapitalization: TextCapitalization.sentences,
              onChanged: (newVal) {
                currTask = newVal;
              },
              decoration: InputDecoration(
                focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0.5),
                ),
                hintText: Kunci.title_here.tr(),
                contentPadding: EdgeInsets.all(10),
                border: OutlineInputBorder(
                  borderSide: BorderSide(width: 0.5),
                ),
              ),
            ),
            SizedBox(height: 5),
            TextField(
              maxLines: 3,
              controller: deskController,
              autofocus: false,
              textCapitalization: TextCapitalization.sentences,
              onChanged: (newVal) {
                deskTask = newVal;
              },
              decoration: InputDecoration(
                focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 0.5),
                ),
                hintText: Kunci.desc_here.tr(),
                contentPadding: EdgeInsets.all(10),
                border: OutlineInputBorder(
                  borderSide: BorderSide(width: 0.5),
                ),
              ),
            ),
            SizedBox(height: 10.0),
            SwitchListTile(
              contentPadding: EdgeInsets.all(0),
              value: remindMe,
              title: Text(Kunci.reminder.tr()),
              onChanged: (newValue) async {
                if (newValue) {
                  reminderDate = await showDatePicker(
                    context: context,
                    initialDate: DateTime.now(),
                    firstDate: DateTime.now(),
                    lastDate: DateTime(DateTime.now().year + 2),
                  );

                  if (reminderDate == null) {
                    return;
                  }

                  reminderTime = await showTimePicker(
                      context: context, initialTime: TimeOfDay.now());

                  if (reminderDate != null && reminderTime != null) {
                    remindMe = newValue;
                  }
                } else {
                  reminderDate = null;
                  reminderTime = null;
                  remindMe = newValue;
                }

                print(reminderTime);
                print(reminderDate);

                setState(() {});
              },
            ),
            Container(
                child: remindMe
                    ? Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Icon(FlutterIcons.calendar_ant, size: 20),
                          SizedBox(width: 5),
                          Text(
                            DateTime(
                              reminderDate.year,
                              reminderDate.month,
                              reminderDate.day,
                              reminderTime.hour,
                              reminderTime.minute,
                            ).toString().substring(0, 10),
                            style: TextStyle(fontSize: 15),
                          ),
                          SizedBox(width: 15),
                          Icon(FlutterIcons.calendar_ant, size: 20),
                          SizedBox(width: 5),
                          Text(
                            DateTime(
                              reminderDate.year,
                              reminderDate.month,
                              reminderDate.day,
                              reminderTime.hour,
                              reminderTime.minute,
                            ).toString().substring(11, 16),
                            style: TextStyle(fontSize: 15),
                          ),
                        ],
                      )
                    : null),
            SizedBox(
              height: remindMe ? 10.0 : 0.0,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Expanded(
                  child: FlatButton(
                    child: Text(
                      Kunci.cancel.tr(),
                      style: TextStyle(color: Colors.white, letterSpacing: 1.5),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    color: Colors.grey,
                  ),
                ),
                SizedBox(width: 10),
                Expanded(
                  child: FlatButton(
                    child: Text(
                      Kunci.save.tr(),
                      style: TextStyle(color: Colors.white, letterSpacing: 1.5),
                    ),
                    onPressed: () async {
                      if (remindMe) {
                        **var scheduledNotificationDateTime = reminderDate
                            .add(Duration(
                                hours: reminderTime.hour,
                                minutes: reminderTime.minute))
                            .subtract(Duration(seconds: 5));
                        var androidPlatformChannelSpecifics =
                            AndroidNotificationDetails(
                          currTask,
                          'To Do Notification',
                          'Do the task',
                          sound: RawResourceAndroidNotificationSound('pop'),
                          priority: Priority.high,
                          importance: Importance.high,
                          playSound: true,**
                        );
                        var iOSPlatformChannelSpecifics =
                            IOSNotificationDetails();
                        NotificationDetails platformChannelSpecifics =
                            NotificationDetails(
                                android: androidPlatformChannelSpecifics,
                                iOS: iOSPlatformChannelSpecifics);
                        id = Provider.of<TaskData>(context, listen: false)
                            .tasks
                            .length;
                        print(id);
                        await flutterLocalNotificationsPlugin.schedule(
                            id,
                            Kunci.task_reminder.tr(),
                            currTask,
                            scheduledNotificationDateTime,
                            platformChannelSpecifics);
                      }

                      Provider.of<TaskData>(
                        context,
                        listen: false,
                      ).addTask(Task(
                        title: currTask,
                        deskripsi: deskTask,
                        isChecked: false,
                        reminderDate: reminderDate == null
                            ? null
                            : reminderDate.add(Duration(
                                hours: reminderTime.hour,
                                minutes: reminderTime.minute,
                              )),
                        reminderId: reminderDate != null ? id : null,
                      ));
                      Navigator.pop(context);
                    },
                    color: Theme.of(context).accentColor,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

please adjust it with your code

Ary Anzh
  • 406
  • 3
  • 15
  • 2
    You could have just posted the `AndroidNotificationDetails`. However, they're only slightly different to the ones in the question and there's no explanation for not including the `Priority` and `Importance` params. – Chris Mar 03 '22 at 13:54