0

使用 Flutter Local Notification for Android 的通知声音在这里根本不起作用是代码

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,
          ),
        ),

我在这个路径中有pop.mp3:\android\app\src\main\res\raw\pop.mp3

如何播放声音?

4

3 回答 3

0

我以前遇到过像你这样的问题,但是通过应用下面的代码完成了

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,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

请使用您的代码进行调整

于 2021-09-03T03:01:55.203 回答
0

我的问题是我在小米上测试应用程序,我意识到小米有一些问题,默认情况下不允许通知声音。不知何故,它在模拟器上也不起作用,但通过在其他物理设备上测试它就可以了。

于 2021-09-05T11:36:55.597 回答
-1

在 Manifest.xml 中添加此代码

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
....
</application>
于 2021-09-01T16:01:48.360 回答