Giới thiệu
Hướng dẫn này minh họa cách xóa (cancel) một thông báo khỏi thanh trạng thái (notification bar) một cách lập trình (programmatically) trong Android. Chúng ta sẽ tạo thông báo tùy chỉnh (custom layout) và cung cấp nút để xóa nó.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Thiết kế giao diện chính (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:id="@+id/btnCreateNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:onClick="createNotification"
android:text="Tạo thông báo" />
</RelativeLayout>
Bước 3: Tạo layout tùy chỉnh cho thông báo (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="Đang kiểm tra"
android:textColor="#000"
android:textSize="18sp" />
<TextView
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="Đây chỉ là thông báo kiểm tra"
android:inputType="text"
android:textSize="14sp" />
</RelativeLayout>
Bước 4: Viết code MainActivity
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import androidx.appcompat.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_CHANNEL_ID = "default";
private NotificationManager notificationManager;
private int notificationId = 0;
@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 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID);
builder.setContent(contentView);
builder.setSmallIcon(R.drawable.ic_launcher_foreground);
builder.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "TÊN_KÊNH_THÔNG_BÁO", importance);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
notificationId = (int) System.currentTimeMillis();
if (notificationManager != null) {
notificationManager.notify(notificationId, builder.build());
}
}
public void removeNotification(View view) {
if (notificationId != 0 && notificationManager != null) {
notificationManager.cancel(notificationId);
}
}
}
Bước 5: Cấu hình 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>
Chạy ứng dụng
Kết nối thiết bị Android thật, chạy ứng dụng từ Android Studio. Nhấn "Tạo thông báo" rồi nhấn "Xóa thông báo" để kiểm tra.
