การแจ้งเตือน
มีปุ่ม 3 ปุ่ม
- ปุ่มที่ 1 Notify เป็นการแจ้งเตือน
- ปุ่มที่ 2 Update เป็นการอัพเดทข้อความแจ้งเตือน
- ปุ่มที่ 3 Cancel เป็นการยกเลิกการแจ้งเตือน
เมื่อผู้ใช้กดที่การแจ้งเตือน จะเรียกหน้า Activity
ที่เราเตรียมไว้ให้แสดงขึ้นมา (หน้า NotificationActivity)
strings.xml
<resources>
<string name="app_name">Music Player</string>
<string name="channel_name">My Channel</string>
<string name="channel_description">My notify channel</string>
</resources>
แก้ไขหน้า activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/notify_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Notify" />
<Button
android:id="@+id/update_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Update" />
<Button
android:id="@+id/cancel_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Cancel" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
แก้ไขหน้า MainActivity.java
package com.phaisarn.musicplayer;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private final int NOTIFY_ID = 0;
private final String CHANNEL_ID = "ABC";
private NotificationCompat.Builder mNotifyBuilder;
private NotificationManager mNotifyManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
(findViewById(R.id.notify_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(
MainActivity.this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, intent, 0);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.myicon);
mNotifyBuilder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setTicker("New Alert")
.setSmallIcon(R.drawable.myicon)
.setLargeIcon(largeIcon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyManager.notify(NOTIFY_ID, mNotifyBuilder.build());
}
});
(findViewById(R.id.update_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotifyBuilder.setContentTitle("Update");
mNotifyBuilder.setContentText("This is an update text.");
mNotifyManager.notify(NOTIFY_ID, mNotifyBuilder.build());
}
});
(findViewById(R.id.cancel_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotifyManager.cancel(NOTIFY_ID);
}
});
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
บรรทัดที่ 25: สร้าง Notification
บรรทัดที่ 53: อัพเดท Notification
บรรทัดที่ 64: ยกเลิก Notification
สร้าง Activity ขึ้นมาใหม่แบบ Empty ชื่อ NotificationActivity
แก้ไขหน้า activity_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotificationActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is your notification"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
แก้ไขหน้า NotificationActivity.java
package com.phaisarn.musicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class NotificationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
}
}

เพิ่มไฟล์รูป myicon.png ไว้ใน
ProjectName\app\src\main\res\drawable\
เมื่อรันแล้วจะได้หน้าตาแบบนี้

เมื่อกดปุ่ม Notify

เมื่อกดปุ่ม Update

เมื่อกดปุ่ม Cancel
การแจ้งเตือนก็จะหายไป