颤振本地通知声音不起作用

Posted

技术标签:

【中文标题】颤振本地通知声音不起作用【英文标题】:Flutter Local Notification Sound not working 【发布时间】:2021-10-31 00:03:12 【问题描述】:

使用 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

如何播放声音?

【问题讨论】:

【参考方案1】:

我之前也遇到过类似你的问题,但是通过应用下面的代码解决了

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

请使用您的代码进行调整

【讨论】:

【参考方案2】:

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

【讨论】:

我收到 Play Store cmets 说某些设备上的通知声音不起作用,我认为可能是小米设备,您找到解决方法了吗?【参考方案3】:

在 Manifest.xml 中添加此代码

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
....
</application>

【讨论】:

请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。

以上是关于颤振本地通知声音不起作用的主要内容,如果未能解决你的问题,请参考以下文章

本地通知声音不起作用

自定义本地通知声音不起作用

颤动的 FCM 通知声音在发布中不起作用

Ionic 3 本地通知 LED/声音在 Android 中不起作用

本地通知的自定义声音不起作用

科尔多瓦本地通知声音在 ios 和 Android 中不起作用