有没有办法让flutter本地通知持续在android中播放声音,除非通知被点击?

bfrts1fy  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(190)

我在网上查了一下,有人建议把AndroidNotificationDetailsadditionalFlags选项的值设为4。
这就是我所做的

AndroidNotificationDetails(
      'notif',
      'notif',
      'Notification',
      icon: '@mipmap/launcher',
      priority: Priority.high,
      sound: RawResourceAndroidNotificationSound('notif'),
      largeIcon: DrawableResourceAndroidBitmap('@mipmap/launcher'),
      additionalFlags: Int32List.fromList(<int>[4]),
      importance: Importance.max,
      playSound: true,
    );

但是当我向下滑动以查看通知列表时,声音停止播放。我希望声音继续播放,除非我点击该特定通知(而不仅仅是向下滑动以查看通知列表)?

ee7vknir

ee7vknir1#

您可以通过使用insistentFlag和androidAllowWhileIdle标志来持久保存声音(或在本例中为警报)。
以下示例使用Flutter本地通知v.12

static Future<void> showFullScreenNotification(
      Alarm alarm, tz.TZDateTime date) async {

    const int insistentFlag = 4;

    final Int64List vibrationPattern = Int64List(4);
    vibrationPattern[0] = 0;
    vibrationPattern[1] = 4000;
    vibrationPattern[2] = 4000;
    vibrationPattern[3] = 4000;

    AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
      alarm.id.toString(),
      'scheduled_alarm_channel',
      channelDescription: 'scheduled_alarm',
      priority: Priority.high,
      importance: Importance.high,
      additionalFlags: Int32List.fromList(<int>[insistentFlag]),
      playSound: true,
      audioAttributesUsage: AudioAttributesUsage.alarm,
      vibrationPattern: vibrationPattern,
    );

    NotificationDetails details =
    NotificationDetails(android: androidPlatformChannelSpecifics);

    await flutterLocalNotificationsPlugin.zonedSchedule(
      ...
      date,
      androidAllowWhileIdle: true,
      ...
    );
  }

相关问题