この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
Androidアプリで通知(上にぴょこんと出るアレ)が表示されるとき、一緒に音も鳴ります。 この音を変更してアプリ内蔵の音を使ってみようと思い、サンプル実装をしてみました。
環境
- Android Studio 3.6
- Pixcel 3a (Android 10)
サンプルアプリを作ってみる
プロジェクトの新規作成
Empty Activity
を選択して作成します。
ボタンを作成する
activity_main.xml
に用意されているTextView
をButton
に変更します。
動作確認用として、コレを押すことで通知を発生させます。
activity_main.xml
<Button
android:id="@+id/notification_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通知する"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
できたボタンは下記です。
通知を作成する
公式ドキュメントを参考にして、MainActivity.kt
を下記にします。
MainActivity.kt
class MainActivity : AppCompatActivity() {
companion object {
const val CHANNEL_ID = "111"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createNotificationChannel()
notification_button.setOnClickListener {
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("通知テスト")
.setContentText("寿限無寿限無五劫の擦り切れ...")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
with(NotificationManagerCompat.from(this)) {
notify(9999, builder.build())
}
}
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"お知らせ",
NotificationManager.IMPORTANCE_DEFAULT).apply {
description = "お知らせを通知します。"
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
}
実際に動かすと、次のような通知が来ました!
このとき、デフォルトの「ピコーン」的な音が鳴っています。
任意の音を鳴らす
任意の音源をres/raw/sample_sound.mp3
として追加します。
そのあと、MainActivity.kt
のcreateNotificationChannel()
に音の設定を追加しました。
MainActivity.kt
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
.build()
val uri = Uri.parse("android.resource://$packageName/${R.raw.sample_sound}")
val channel = NotificationChannel(
CHANNEL_ID,
"お知らせ",
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = "お知らせを通知します。"
setSound(uri, audioAttributes)
}
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
動作確認する
チャンネルIDが同じであるため、アプリを一度アンイストールしておきます。アンイストールしたくない場合は、チャンネルIDを変更すればOKです。
その後、ボタンを押すと追加した音源が鳴りつつ通知されました!!
さいごに
思っていたよりも簡単にできました。 あと、サイレントモードだと音が鳴らないことに気づくのに時間がかかりました……。
参考
- 通知を作成する | Android デベロッパー | Android Developers
- AudioAttributes | Android デベロッパー | Android Developers
- AudioAttributes.Builder | Android デベロッパー | Android Developers
- NotificationChannel | Android デベロッパー | Android Developers
- push通知の通知音を変更する - Qiita
- アンドロイドのres/rawフォルダに格納されている.mp3ファイルのURIを取得する
- android - Notification sound on API 26 - Stack Overflow