Androidでロック画面への通知を非表示にしつつ、通知領域には表示させる方法
本記事では、Androidアプリにおいてロック画面には通知を表示させず、通知領域(通知ドロワー)には通常どおり表示させる方法を、実際に動作するサンプルコードとともに解説します。
実装のポイント
鍵となるのは、NotificationCompat.Builder の setVisibility() メソッドです。ここに VISIBILITY_SECRET を指定すると、その通知はロック画面上に一切表示されなくなります。一方、ロック解除後の通知領域にはこれまでどおり表示されるため、個人情報や機密性の高い内容を含む通知を扱うアプリに適した手法です。
さらに KeyguardManager を使って端末がロック中かどうかを判定し、ロック中の場合は通知の優先度を PRIORITY_MIN に下げることで、ヘッドアップ表示などの目立ちやすい演出も抑制できます。
手順1:新規プロジェクトの作成
Android Studioを起動し、「File → New Project」から新しいプロジェクトを作成します。必要な項目を入力してプロジェクトをセットアップしましょう。
手順2:res/layout/activity_main.xml の編集
メインレイアウトに、通知を作成するためのボタンを配置します。以下のコードを追加してください。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:onClick="createNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:text="Create notification" />
</RelativeLayout>
手順3:res/layout/custom_notification_layout.xml の作成
カスタム通知用のレイアウトファイルを新規作成し、アイコン・タイトル・テキスト入力欄を持つ独自の通知デザインを定義します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="96dp"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_marginEnd="10dp"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/image"
android:text="Testing"
android:textColor="#000"
android:textSize="18sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="8dp"
android:layout_toEndOf="@+id/image"
android:hint="Enter something..."
android:inputType="text"
android:textSize="14sp" />
</RelativeLayout>
手順4:src/MainActivity.java の実装
メインアクティビティに、通知生成処理を記述します。
package app.tutorialspoint.com.notifyme;
import android.app.KeyguardManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;
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);
onNewIntent(getIntent());
}
public void createNotification(View view) {
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContent(contentView);
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
mBuilder.setVisibility(NotificationCompat.VISIBILITY_SECRET);
KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardLocked()) mBuilder.setPriority(NotificationCompat.PRIORITY_MIN);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
}
このコードにおける重要なポイントは次の3点です。
- setVisibility(VISIBILITY_SECRET):ロック画面上から通知を完全に非表示にします。
- isKeyguardLocked() の判定:端末がロック中の場合のみ優先度を PRIORITY_MIN に設定し、通知の存在感を抑えます。
- 通知チャンネルの作成:Android 8.0(API 26)以降では、すべての通知をいずれかのチャンネルに割り当てる必要があります。
なお、上記コードはサポートライブラリ(android.support)を使用しています。最新のAndroid Studioで新規プロジェクトを作成した場合は、AndroidX(androidx.core.app.NotificationCompat など)に読み替えてください。
手順5:AndroidManifest.xml の設定
マニフェストファイルに以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.notifyme">
<uses-permission android:name="android.permission.VIBRATE" />
<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スマートフォンをパソコンに接続しているものとして説明します。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。接続した端末を選択して実行すると、端末にアプリの初期画面が表示されます。

ボタンをタップして通知を作成したら、一度画面をロックしてみてください。ロック画面には通知が表示されず、ロック解除後に通知領域を下へスワイプすると、カスタムレイアウトの通知が届いていることを確認できます。これで、ロック画面での通知を抑制しつつ、通知領域には確実に通知を残す実装が完成です。
-
Androidのロック画面で通知を非表示にする方法【機種別設定手順】
ロック画面で通知をチェックできるのはとても便利です。内容をひと目で確認でき、その通知が重要なものかどうか、スマホのロックを解除する価値があるかどうかを判断できます。 しかし一方で、ロック画面に通知内容がそのまま表示されるということは、意図せず個人情報を周囲の人に見られてしまうリスクも伴います。メッセージの内容やアプリの通知詳細は、他人に知られるべきものではありません。そこで本記事では、Androidのロック画面から通知を非表示にする方法を詳しく解説します。root化は不要で、設定も思ったより簡単です。 ロック画面のすべての通知を非表示にする方法 Android端末のロック画面から機密性の高い
-
Androidのロック画面をカスタマイズする方法|ロック方式・壁紙・通知設定を徹底解説
Androidスマートフォンが世界中のユーザーに支持されている理由のひとつに、自由度の高いカスタマイズ性が挙げられます。アプリドロワー、通知パネル、ランチャーなど、好みに合わせてさまざまな要素を変更できるのが魅力です。 ところが、意外と見落とされがちなのが「ロック画面」の存在です。ロック画面とは、電源を入れたときに最初に表示される画面であり、その名の通り、スマートフォンを使う前に解除が必要となる画面のことです。 Androidのロック画面には、日常の利便性を高めるための多彩なオプションが用意されています。本記事では、ぜひ試していただきたいカスタマイズ方法をわかりやすくご紹介します。 ロッ