Androidで通知チャネルを作成・管理する方法を解説
本記事では、Androidアプリにおける通知チャネル(Notification Channel)の作成と管理方法を、実際のコード例とともにわかりやすく解説します。
通知チャネルはAndroid 8.0(APIレベル26)以降で導入された機能で、通知のカテゴリごとに音・振動・表示方法などの挙動を細かく制御できる仕組みです。ユーザー側でもチャネル単位で通知のオン/オフを設定できるため、現代のAndroidアプリ開発では必須の知識となっています。
ステップ1: Android Studioで新規プロジェクトを作成する
まず、Android Studioを開き、「File」⇒「New Project」を選択して、必要な項目をすべて入力し、新しいプロジェクトを作成します。
ステップ2: activity_main.xmlにコードを追加する
次に、res/layout/activity_main.xmlに以下のコードを追加します。ここでは、通知を作成するためのボタンを画面中央に1つ配置しています。
<? xml version= "1.0" encoding= "utf-8" ?> <android.support.constraint.ConstraintLayout xmlns: android = "https://schemas.android.com/apk/res/android" xmlns: app = "https://schemas.android.com/apk/res-auto" xmlns: tools = "https://schemas.android.com/tools" android :layout_width = "match_parent" android :layout_height = "match_parent" android :padding = "16dp" tools :context = ".MainActivity" > <Button android :id = "@+id/btnCreateNotification" android :layout_width = "0dp" android :layout_height = "wrap_content" android :text = "Create notification" app :layout_constraintBottom_toBottomOf = "parent" app :layout_constraintEnd_toEndOf = "parent" app :layout_constraintStart_toStartOf = "parent" app :layout_constraintTop_toTopOf = "parent" /> </android.support.constraint.ConstraintLayout>
ステップ3: サウンドファイルをrawフォルダに追加する
カスタム通知音を使用するため、resディレクトリ直下に「raw」フォルダを作成し、そこにサウンドファイル(本例では quite_impressed.mp3)を格納します。

ステップ4: MainActivity.javaにコードを追加する
src/MainActivity.javaに以下のコードを追加します。ボタンがクリックされると、通知チャネルが生成され、カスタムサウンド付きの通知が表示される仕組みです。
package app.tutorialspoint.com.notifyme ;
import android.app.NotificationChannel ;
import android.app.NotificationManager ;
import android.content.ContentResolver ;
import android.content.Context ;
import android.graphics.Color ;
import android.media.AudioAttributes ;
import android.net.Uri ;
import android.support.v4.app.NotificationCompat ;
import android.support.v7.app.AppCompatActivity ;
import android.os.Bundle ;
import android.view.View ;
import android.widget.Button ;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
private final static String default_notification_channel_id = "default" ;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main ) ;
Button btnCreateNotification = findViewById(R.id. btnCreateNotification ) ;
btnCreateNotification.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick (View v) {
Uri sound = Uri. parse (ContentResolver. SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/quite_impressed.mp3" ) ;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity. this,
default_notification_channel_id )
.setSmallIcon(R.drawable. ic_launcher_foreground )
.setContentTitle( "Test" )
.setSound(sound)
.setContentText( "Hello! This is my first push notification" );
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context. NOTIFICATION_SERVICE ) ;
if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes. CONTENT_TYPE_SONIFICATION )
.setUsage(AudioAttributes. USAGE_ALARM )
.build() ;
int importance = NotificationManager. IMPORTANCE_HIGH ;
NotificationChannel notificationChannel = new
NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
notificationChannel.enableLights( true ) ;
notificationChannel.setLightColor(Color. RED ) ;
notificationChannel.enableVibration( true ) ;
notificationChannel.setVibrationPattern( new long []{ 100 , 200 , 300 , 400 , 500 , 400 , 300 , 200 , 400 }) ;
notificationChannel.setSound(sound , audioAttributes) ;
mBuilder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel) ;
}
assert mNotificationManager != null;
mNotificationManager.notify(( int ) System. currentTimeMillis (), mBuilder.build()) ;
}
}) ;
}
}コードのポイント
- NotificationChannelの生成: チャネルID・表示名・重要度(IMPORTANCE_HIGH)を指定してインスタンスを作成します。
- enableLights()/setLightColor(): 通知時のLEDライトの点滅を有効化し、色を赤に設定しています。
- enableVibration()/setVibrationPattern(): 振動を有効にし、独自の振動パターン(ミリ秒単位の配列)を適用します。
- setSound(): AudioAttributesと組み合わせて、rawフォルダ内のカスタムサウンドをチャネルに紐付けます。
- バージョンチェック: Build.VERSION_CODES.O以降(Android 8.0以上)でのみチャネル作成処理を実行することで、旧バージョンとの互換性を保っています。
ステップ5: AndroidManifest.xmlにコードを追加する
最後に、androidManifest.xmlに以下のコードを記述します。
<? xml version = "1.0" encoding = "utf-8" ?> <manifest xmlns: android = "https://schemas.android.com/apk/res/android" package = "app.tutorialspoint.com.notifyme" > <application android :allowBackup = "true" android :icon = "@mipmap/ic_launcher" android :label = "@string/app_name" android :roundIcon = "@mipmap/ic_launcher_round" android :supportsRtl = "true" android :theme = "@style/AppTheme" > <activity android :name = ".MainActivity" > <intent-filter> <action android :name = "android.intent.action.MAIN" /> <category android :name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アプリケーションの実行
それでは、アプリケーションを実行してみましょう。実機のAndroidスマートフォンがPCに接続されているものとして進めます。Android Studioからアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。表示された選択肢から自分のモバイルデバイスを選ぶと、端末の画面が立ち上がります。

画面上の「Create notification」ボタンをタップすると、設定したカスタムサウンドと振動パターンで通知が表示されます。これで、Androidにおける通知チャネルの作成と管理の基本を習得できました。
-
iPhoneで連絡先グループを作成・管理する方法【iCloud・アプリ対応】
頻繁に連絡を取る相手が多い場合、連絡先を効率的に管理する方法を見つけることが大切です。個人ごとではなく、グループ単位で定期的にやり取りする機会が多いなら、連絡先グループを設定するのが非常に有効な手段となります。連絡先を整理しやすくなるだけでなく、必要なときにすぐ目的の相手へ連絡できるようになるのもメリットです。この記事では、iPhoneで連絡先グループを作成・管理する方法を詳しく解説します。集まりやグループ旅行の計画を立てるとき、一人ひとりに個別にメッセージを送るのはかなりの時間がかかります。しかし残念ながら、iOSにはiPhone上で直接グループを作成する簡単な機能が用意されていません。幸い
-
Androidのデータ使用量を管理・追跡する方法|「Check Data Usage」で通信量を賢く節約
何か調べものをしたいとき、私たちはスマートフォンのロックを解除してGoogleに答えを求めます。それだけではありません。オンライン決済、友人とのビデオ通話、タクシーの配車予約、ネットショッピング、お気に入りの動画視聴など、スマホは生活のあらゆる場面で活躍しています。しかし、こうした活動はすべてモバイルデータを大量に消費し、気づけば携帯電話の請求額が膨らんでいることも少なくありません。だからこそ、自分のデータ使用量をしっかりと把握しておくことが非常に重要なのです。 そのためには、データ管理アプリを活用するのがおすすめです。Google Playには、データ使用量を管理・監視して上限を守り、使いす