0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

local_notifications オリジナルの通知音の保存場所

Posted at

local_notificationsで、通知音をオリジナルの音源に差し替えました。
その方法についてメモです。

###環境

  • macOS Catalina 10.15.7
  • Flutter 2.2.2
  • flutter_local_notifications ^8.1.1

##Android
flutterのフォルダで以下の場所にファイルを置きます。
android/app/src/main/res/raw/sound.mp3

####Androidでサポートされているメディア形式
https://developer.android.com/guide/topics/media/media-formats.html

##iOS

  • XcodeでRunner.xcodeprojを開き、左のツリーを右クリックしNew Groupe を「Resouces」という名前で作成
  • Xcode上のResoucesにサウンドファイルをドラッグ&ドロップ
  • オプションのダイアログで「Create folder refernces」「Runner」にチェック

####iOSでサポートされているメディア形式
https://developer.apple.com/documentation/usernotifications/unnotificationsound?language=objc

###local_notificationsに関わるソースはこんな感じ

Future<void> _scheduleNotification(
      int id, String title, String body, DateTime time) async {
    var scheduleNotificationDateTime = time;

    final timeZone = TimeZone();
    String timeZoneName = await timeZone.getTimeZoneName();
    final location = await timeZone.getLocation(timeZoneName);
    scheduleNotificationDateTime =
        tz.TZDateTime.from(scheduleNotificationDateTime, location);

    var androidChannelSpecifics = AndroidNotificationDetails(
      id.toString(),
      title,
      body,
      icon: 'app_icon',
      sound: RawResourceAndroidNotificationSound('sound'),//←Android用に保存した拡張子抜きのファイル名
      enableLights: true,
      color: const Color.fromARGB(255, 255, 0, 0),
      ledColor: const Color.fromARGB(255, 255, 0, 0),
      ledOnMs: 1000,
      ledOffMs: 500,
      importance: Importance.max,
      priority: Priority.high,
      playSound: true,
      timeoutAfter: 10000,
      styleInformation: DefaultStyleInformation(true, true),
    );

    var iosChannelSpecifics = IOSNotificationDetails(
      presentSound: true,
      presentBadge: true,
      presentAlert: true,
      sound: 'sound.caf',//←Xcodeにドロップしたファイル名
    );

    var platformChannelSpecifics = NotificationDetails(
      android: androidChannelSpecifics,
      iOS: iosChannelSpecifics,
    );

    await flutterLocalNotificationsPlugin.zonedSchedule(
      id,
      title,
      body,
      scheduleNotificationDateTime,
      platformChannelSpecifics,
      payload: 'payload 001',
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.wallClockTime,
    );
  }

####参考
https://youtu.be/X3tAGnGc_t0

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?