Androidアプリで通知アイコンを設定する方法【初心者向け解説】
このチュートリアルでは、Androidアプリで通知(Notification)を表示し、通知アイコンを設定する方法をステップバイステップで解説します。サンプルコードもすべて掲載しているので、そのままコピーして試すことができます。
全体の流れ
今回作成するのは、ボタンをタップすると通知が表示されるシンプルなアプリです。通知には小さいアイコン(smallIcon)、タイトル、本文テキストを設定します。
手順1:Android Studioで新規プロジェクトを作成する
まず、Android Studioを開き、メニューから「File」⇒「New Project」を選択して新しいプロジェクトを作成します。プロジェクト名やパッケージ名など必要な項目を入力しましょう。
手順2:res/layout/activity_main.xml にレイアウトを記述する
次に、画面レイアウトファイル activity_main.xml に以下のコードを追加します。ここでは、画面中央に「Create notification」というボタンを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:src/MainActivity.java に通知処理を実装する
続いて、MainActivity.java に以下のコードを記述します。ポイントは NotificationCompat.Builder を使って通知を組み立てる部分です。setSmallIcon() メソッドに指定した drawable リソースが、ステータスバーに表示される通知アイコンになります。
package app.tutorialspoint.com.notifyme;
import android.app.NotificationManager;
import android.content.Context;
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 {
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) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this,
default_notification_channel_id)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Test")
.setContentText("Hello! This is my first push notification");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
});
}
}補足:Android 8.0以降での注意点
Android 8.0(APIレベル26)以降をターゲットとする場合、通知を表示する前に NotificationChannel を作成しておく必要があります。チャンネルが未登録の状態で通知IDを渡しても、通知は表示されません。Application クラスや Activity の onCreate 内で以下のようにチャンネルを作成しておきましょう。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
default_notification_channel_id,
"Default Channel",
NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}手順4:androidManifest.xml の内容を確認する
最後に、AndroidManifest.xml を確認します。基本的な設定は以下の通りです。特別なパーミッションは不要ですが、ランチャーアクティビティとして MainActivity が正しく宣言されていることをチェックしてください。
<?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にUSB接続している前提で進めます。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの実行(Run)アイコンをクリックします。接続した端末を選択してアプリをインストールすると、次のようなデフォルト画面が表示されます。

画面上の「Create notification」ボタンをタップすると、ステータスバーに設定したアイコンとともに通知が表示されます。ステータスバーを下にスワイプして、通知ドロワーに「Test」「Hello! This is my first push notification」という内容の通知が届いていることを確認してみてください。
まとめ
通知アイコンの設定は、NotificationCompat.Builder の setSmallIcon() にdrawableリソースを渡すだけで実現できます。白一色のシンプルなアイコン(VectorDrawableなど)を用意すると、Androidのデザインガイドラインに沿った見栄えの良い通知になります。ぜひ本記事のサンプルコードを参考に、自分のアプリに通知機能を組み込んでみてください。
-
Android 12でアプリアイコンの形を変更する方法|Material Youとサードパーティアプリ活用術
Androidスマホのアイコンの形を変えるのは、手軽に個性を出せるカスタマイズ方法として人気があります。しかしAndroid 12からは、アイコンの形を変更するオプションがすべて削除されてしまったように見えます。もうAndroidでアイコンをカスタマイズできないのでしょうか?実は、そうとも限りません。この記事では、Android 12のMaterial You環境下でアイコンの見た目を変更する方法と、より柔軟なカスタマイズを可能にする追加オプションをご紹介します。Material You:「個人向け」とは言い難い個人設定残念ながら、まず悪いニュースからお伝えします。純正のAndroid 12
-
【Android】アプリアイコンバッジの有効化・無効化を切り替える方法
Androidスマートフォンを使っている方なら、通知を受け取るとロック画面にアラートとして表示されることをご存じでしょう。ロックを解除して通知シェードを下にスワイプすれば、通知内容を簡単に確認できます。さらに、LEDライトを点灯させて通知をお知らせする機能を利用している方も多いはずです。しかし、アプリアイコンバッジですべての未読通知を確認したい場合、多くのAndroid端末ではこの機能がデフォルトで有効になっていません。アプリアイコンバッジ機能とは、各アプリのアイコン上に、そのアプリの未読通知件数を数字で表示する機能です。iPhoneユーザーにはおなじみの機能で、iOSでは標準搭載されています