การแจ้งเตือนจะให้ข้อมูลสั้นๆ ที่ทันท่วงทีเกี่ยวกับเหตุการณ์ในแอปขณะที่ไม่ได้ใช้งาน เอกสารนี้แสดงวิธีสร้างการแจ้งเตือนด้วย ฟีเจอร์ต่างๆ ดูข้อมูลเบื้องต้นเกี่ยวกับลักษณะที่การแจ้งเตือนปรากฏใน Android ได้ที่ภาพรวมของการแจ้งเตือน ดูโค้ดตัวอย่างที่ใช้การแจ้งเตือนได้ในตัวอย่าง SociaLite ใน GitHub
โค้ดในหน้านี้ใช้
NotificationCompat
API จากไลบรารี AndroidX API เหล่านี้ช่วยให้คุณเพิ่มฟีเจอร์ที่ใช้ได้เฉพาะใน Android เวอร์ชันใหม่กว่า
ขณะที่ยังคงความเข้ากันได้กับ Android 9 (API ระดับ 28) อย่างไรก็ตาม ฟีเจอร์บางอย่าง เช่น การดำเนินการตอบกลับในบรรทัด
จะไม่มีผลในเวอร์ชันก่อนหน้า
เพิ่ม AndroidX Core Library
แม้ว่าโปรเจ็กต์ส่วนใหญ่ที่สร้างด้วย Android Studio จะมีทรัพยากร Dependency ที่จำเป็น
ในการใช้ NotificationCompat
แต่ให้ตรวจสอบว่าไฟล์ build.gradle
ระดับโมดูลมีทรัพยากร Dependency ต่อไปนี้
Groovy
dependencies { implementation "androidx.core:core-ktx:1.16.0" }
Kotlin
dependencies { implementation("androidx.core:core-ktx:1.16.0") }
สร้างการแจ้งเตือนพื้นฐาน
การแจ้งเตือนในรูปแบบพื้นฐานและกะทัดรัดที่สุด หรือที่เรียกว่ารูปแบบยุบจะแสดงไอคอน ชื่อ และเนื้อหาข้อความจำนวนเล็กน้อย ส่วนนี้แสดงวิธีสร้างการแจ้งเตือนที่ผู้ใช้แตะเพื่อเปิด กิจกรรมในแอปได้
รูปที่ 1 การแจ้งเตือนที่มี ไอคอน ชื่อ และข้อความบางส่วน
ดูรายละเอียดเพิ่มเติมเกี่ยวกับส่วนต่างๆ ของการแจ้งเตือนได้ที่โครงสร้างของการแจ้งเตือน
ประกาศสิทธิ์รันไทม์
Android 13 (API ระดับ 33) ขึ้นไปรองรับสิทธิ์รันไทม์สำหรับการโพสต์การแจ้งเตือนที่ไม่ได้รับการยกเว้น (รวมถึงบริการที่ทำงานอยู่เบื้องหน้า (FGS)) จากแอป
สิทธิ์ที่คุณต้องประกาศในไฟล์ Manifest ของแอปจะปรากฏ ในข้อมูลโค้ดต่อไปนี้
<manifest ...> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <application ...> ... </application> </manifest>
ดูรายละเอียดเพิ่มเติมเกี่ยวกับสิทธิ์รันไทม์ได้ที่ สิทธิ์รันไทม์ของการแจ้งเตือน
ตั้งค่าเนื้อหาการแจ้งเตือน
หากต้องการเริ่มต้นใช้งาน ให้ตั้งค่าเนื้อหาและช่องของการแจ้งเตือนโดยใช้ออบเจ็กต์
NotificationCompat.Builder
ตัวอย่างต่อไปนี้แสดงวิธีสร้างการแจ้งเตือนที่มีข้อมูลต่อไปนี้
ไอคอนขนาดเล็กที่ตั้งค่าโดย
setSmallIcon()
นี่เป็นเนื้อหาที่ผู้ใช้มองเห็นได้เพียงอย่างเดียวที่จำเป็นชื่อที่ตั้งโดย
setContentTitle()
ข้อความเนื้อหาที่ตั้งค่าโดย
setContentText()
ลำดับความสำคัญของการแจ้งเตือนที่กำหนดโดย
setPriority()
ลำดับความสำคัญจะเป็นตัวกำหนดระดับการรบกวนของการแจ้งเตือนใน Android 7.1 และ เวอร์ชันก่อนหน้า สำหรับ Android 8.0 ขึ้นไป ให้ตั้งค่าความสำคัญของช่องทางตามที่แสดงในส่วนถัดไปแทน
Kotlin
var builder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle(textTitle) .setContentText(textContent) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
Java
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle(textTitle) .setContentText(textContent) .setPriority(NotificationCompat.PRIORITY_DEFAULT);
ตัวสร้าง NotificationCompat.Builder
กำหนดให้คุณระบุรหัสช่อง
การดำเนินการนี้จำเป็นต่อความเข้ากันได้กับ Android 8.0 (API ระดับ 26) ขึ้นไป แต่เวอร์ชันก่อนหน้าจะละเว้น
โดยค่าเริ่มต้น จะมีการตัดเนื้อหาข้อความของการแจ้งเตือนให้อยู่ภายใน 1 บรรทัด คุณ แสดงข้อมูลเพิ่มเติมได้โดยสร้างการแจ้งเตือนที่ขยายได้
รูปที่ 2 การแจ้งเตือนที่ขยายได้ ในรูปแบบที่ยุบและขยาย
หากต้องการให้การแจ้งเตือนยาวกว่านี้ คุณอาจเปิดใช้การแจ้งเตือนที่ขยายได้โดยเพิ่มเทมเพลตรูปแบบที่มี setStyle()
ตัวอย่างเช่น โค้ดต่อไปนี้จะสร้างพื้นที่ข้อความที่ใหญ่ขึ้น
Kotlin
var builder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Much longer text that cannot fit one line...") .setStyle(NotificationCompat.BigTextStyle() .bigText("Much longer text that cannot fit one line...")) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
Java
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Much longer text that cannot fit one line...") .setStyle(new NotificationCompat.BigTextStyle() .bigText("Much longer text that cannot fit one line...")) .setPriority(NotificationCompat.PRIORITY_DEFAULT);
ดูข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบการแจ้งเตือนขนาดใหญ่อื่นๆ รวมถึงวิธีเพิ่มรูปภาพและการควบคุมการเล่นสื่อได้ที่สร้างการแจ้งเตือนที่ขยายได้
สร้างแชแนลและตั้งค่าความสำคัญ
ก่อนที่จะแสดงการแจ้งเตือนใน Android 8.0 ขึ้นไป ให้ลงทะเบียนช่องทางการแจ้งเตือนของแอปกับระบบโดยส่งอินสแตนซ์ของ NotificationChannel
ไปยัง createNotificationChannel()
โค้ดต่อไปนี้ถูกบล็อกโดยเงื่อนไขในเวอร์ชัน
SDK_INT
Kotlin
private fun createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is not in the Support Library. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val name = getString(R.string.channel_name) val descriptionText = getString(R.string.channel_description) val importance = NotificationManager.IMPORTANCE_DEFAULT val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { description = descriptionText } // Register the channel with the system. val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) } }
Java
private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is 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); } }
เนื่องจากคุณต้องสร้างช่องการแจ้งเตือนก่อนโพสต์การแจ้งเตือนใน Android 8.0 ขึ้นไป ให้เรียกใช้โค้ดนี้ทันทีที่แอปเริ่มทำงาน คุณเรียกใช้ฟังก์ชันนี้ซ้ำๆ ได้อย่างปลอดภัย เนื่องจากระบบจะไม่ดำเนินการใดๆ หากมีการสร้างช่องการแจ้งเตือนที่มีอยู่แล้ว
ตัวสร้าง NotificationChannel
ต้องมี importance
โดยใช้ค่าคงที่ค่าใดค่าหนึ่งจาก
คลาส NotificationManager
พารามิเตอร์นี้จะกำหนดวิธีกระตุ้นให้ผู้ใช้สนใจการแจ้งเตือนที่อยู่ในช่องนี้ กำหนดลำดับความสำคัญด้วย setPriority()
เพื่อรองรับ Android 7.1
และเวอร์ชันก่อนหน้า ดังที่แสดงในตัวอย่างก่อนหน้า
แม้ว่าคุณจะต้องตั้งค่าความสำคัญหรือลำดับความสำคัญของการแจ้งเตือนตามที่แสดงใน ตัวอย่างต่อไปนี้ แต่ระบบก็ไม่รับประกันลักษณะการทำงานของการแจ้งเตือนที่คุณได้รับ ในบางกรณี ระบบอาจเปลี่ยนระดับความสำคัญตามปัจจัยอื่นๆ และผู้ใช้สามารถกำหนดระดับความสำคัญของช่องทางหนึ่งๆ ใหม่ได้เสมอ
ดูข้อมูลเพิ่มเติมเกี่ยวกับความหมายของระดับต่างๆ ได้ที่หัวข้อเกี่ยวกับระดับความสำคัญของการแจ้งเตือน
ตั้งค่าการแตะของการแจ้งเตือน
การแจ้งเตือนทุกรายการต้องตอบสนองต่อการแตะ โดยปกติแล้วจะเป็นการเปิดกิจกรรมในแอปที่สอดคล้องกับการแจ้งเตือน โดยระบุเจตนาของเนื้อหา
ที่กำหนดด้วยออบเจ็กต์ PendingIntent
แล้วส่งไปยัง
setContentIntent()
ข้อมูลโค้ดต่อไปนี้แสดงวิธีสร้าง Intent พื้นฐานเพื่อเปิดกิจกรรม เมื่อผู้ใช้แตะการแจ้งเตือน
Kotlin
// Create an explicit intent for an Activity in your app. val intent = Intent(this, AlertDetails::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE) val builder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) // Set the intent that fires when the user taps the notification. .setContentIntent(pendingIntent) .setAutoCancel(true)
Java
// Create an explicit intent for an Activity in your app. Intent intent = new Intent(this, AlertDetails.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) // Set the intent that fires when the user taps the notification. .setContentIntent(pendingIntent) .setAutoCancel(true);
โค้ดนี้เรียกใช้
setAutoCancel()
ซึ่งจะนำการแจ้งเตือนออกโดยอัตโนมัติเมื่อผู้ใช้แตะ
แฟล็ก Intent ในตัวอย่างก่อนหน้าจะรักษาประสบการณ์การนำทางที่ผู้ใช้คาดหวัง หลังจากที่ผู้ใช้เปิดแอปโดยใช้การแจ้งเตือน คุณอาจต้องใช้ฟีเจอร์นี้ ทั้งนี้ขึ้นอยู่กับประเภทกิจกรรมที่คุณกำลังจะเริ่ม ซึ่งอาจเป็นกิจกรรมใดกิจกรรมหนึ่งต่อไปนี้
กิจกรรมที่มีไว้สำหรับการตอบกลับการแจ้งเตือนเท่านั้น ไม่มีเหตุผลที่ผู้ใช้จะไปยังกิจกรรมนี้ในระหว่างการใช้งานแอปตามปกติ ดังนั้นกิจกรรมจึงเริ่มงานใหม่แทนที่จะเพิ่มลงในงานและสแต็กย้อนกลับที่มีอยู่ของแอป นี่คือ ประเภทของเจตนาที่สร้างขึ้นในตัวอย่างก่อนหน้า
กิจกรรมที่มีอยู่ในโฟลว์แอปปกติของแอป ในกรณีนี้ การเริ่มกิจกรรมจะสร้างสแต็กย้อนกลับเพื่อให้เป็นไปตามความคาดหวังของผู้ใช้ สำหรับปุ่มย้อนกลับและปุ่มขึ้น
ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีต่างๆ ในการกำหนดค่า Intent ของการแจ้งเตือนได้ที่ เริ่มกิจกรรมจากการแจ้งเตือน
แสดงการแจ้งเตือน
หากต้องการให้การแจ้งเตือนปรากฏ ให้เรียกใช้
NotificationManagerCompat.notify()
โดยส่งรหัสที่ไม่ซ้ำกันสำหรับการแจ้งเตือนและผลลัพธ์ของ
NotificationCompat.Builder.build()
ตัวอย่างต่อไปนี้แสดงให้เห็นถึงการดำเนินการนี้
Kotlin
with(NotificationManagerCompat.from(this)) { if (ActivityCompat.checkSelfPermission( this@MainActivity, Manifest.permission.POST_NOTIFICATIONS ) != PackageManager.PERMISSION_GRANTED ) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, // grantResults: IntArray) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return@with } // notificationId is a unique int for each notification that you must define. notify(NOTIFICATION_ID, builder.build()) }
Java
with(NotificationManagerCompat.from(this)) { if (ActivityCompat.checkSelfPermission( this@MainActivity, Manifest.permission.POST_NOTIFICATIONS ) != PackageManager.PERMISSION_GRANTED ) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return } // notificationId is a unique int for each notification that you must define. notify(NOTIFICATION_ID, builder.build()) }
บันทึกรหัสการแจ้งเตือนที่คุณส่งไปยัง NotificationManagerCompat.notify()
เนื่องจากคุณจะต้องใช้รหัสนี้เมื่อต้องการอัปเดตหรือนำการแจ้งเตือนออก
นอกจากนี้ หากต้องการทดสอบการแจ้งเตือนพื้นฐานในอุปกรณ์ที่ใช้ Android 13 ขึ้นไป ให้เปิดการแจ้งเตือนด้วยตนเองหรือสร้างกล่องโต้ตอบเพื่อขอรับการแจ้งเตือน
เพิ่มปุ่มการทำงาน
การแจ้งเตือนจะมีปุ่มการดำเนินการได้สูงสุด 3 ปุ่มที่ช่วยให้ผู้ใช้ตอบกลับได้อย่างรวดเร็ว เช่น เลื่อนการช่วยเตือนหรือตอบกลับข้อความ แต่ปุ่มการดำเนินการเหล่านี้ต้องไม่ซ้ำกับการดำเนินการที่เกิดขึ้นเมื่อผู้ใช้แตะการแจ้งเตือน
รูปที่ 3 การแจ้งเตือนที่มี ปุ่มการทำงาน 1 ปุ่ม
หากต้องการเพิ่มปุ่มการดำเนินการ ให้ส่ง PendingIntent
ไปยังเมธอด
addAction()
ซึ่งคล้ายกับการตั้งค่าการแตะเริ่มต้นของการแจ้งเตือน ยกเว้นว่าคุณจะทำสิ่งอื่นๆ ได้แทนการเปิดกิจกรรม เช่น เริ่ม BroadcastReceiver
ที่ทำงานในเบื้องหลังเพื่อไม่ให้การดำเนินการขัดจังหวะแอปที่เปิดอยู่แล้ว
ตัวอย่างเช่น โค้ดต่อไปนี้แสดงวิธีส่งการออกอากาศไปยังเครื่องรับที่เฉพาะเจาะจง
Kotlin
val ACTION_SNOOZE = "snooze" val snoozeIntent = Intent(this, MyBroadcastReceiver::class.java).apply { action = ACTION_SNOOZE putExtra(EXTRA_NOTIFICATION_ID, 0) } val snoozePendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0) val builder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .addAction(R.drawable.ic_snooze, getString(R.string.snooze), snoozePendingIntent)
Java
String ACTION_SNOOZE = "snooze" Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class); snoozeIntent.setAction(ACTION_SNOOZE); snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0); PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) .addAction(R.drawable.ic_snooze, getString(R.string.snooze), snoozePendingIntent);
ดูข้อมูลเพิ่มเติมเกี่ยวกับการสร้าง BroadcastReceiver
เพื่อเรียกใช้การทำงานเบื้องหลังได้ที่ภาพรวมของการออกอากาศ
หากต้องการสร้างการแจ้งเตือนที่มีปุ่มควบคุมการเล่นสื่อ เช่น หยุดชั่วคราวและข้ามแทร็ก โปรดดูวิธีสร้างการแจ้งเตือนที่มีตัวควบคุมสื่อ
เพิ่มการดำเนินการตอบกลับโดยตรง
การดำเนินการตอบกลับโดยตรงซึ่งเปิดตัวใน Android 7.0 (API ระดับ 24) ช่วยให้ผู้ใช้ป้อนข้อความลงในการแจ้งเตือนได้โดยตรง จากนั้นระบบจะส่งข้อความไปยังแอปของคุณโดยไม่ต้องเปิดกิจกรรม เช่น คุณสามารถใช้การดำเนินการตอบกลับโดยตรง เพื่อให้ผู้ใช้ตอบกลับ SMS หรืออัปเดตรายการงานได้จากภายใน การแจ้งเตือน
รูปที่ 4 การแตะปุ่ม "ตอบ" จะเปิดการป้อนข้อความ
การดำเนินการตอบกลับโดยตรงจะปรากฏเป็นปุ่มเพิ่มเติมในการแจ้งเตือนที่เปิดช่องป้อนข้อความ เมื่อผู้ใช้พิมพ์เสร็จแล้ว ระบบจะแนบข้อความ ตอบกลับไปยัง Intent ที่คุณระบุสําหรับการกระทําในการแจ้งเตือน และส่ง Intent ไปยังแอปของคุณ
เพิ่มปุ่มตอบกลับ
หากต้องการสร้างการดำเนินการแจ้งเตือนที่รองรับการตอบกลับโดยตรง ให้ทำตามขั้นตอนต่อไปนี้
- สร้างอินสแตนซ์ของ
RemoteInput.Builder
ที่คุณเพิ่มลงในการดำเนินการแจ้งเตือนได้ ตัวสร้างของคลาสนี้ยอมรับ สตริงที่ระบบใช้เป็นคีย์สำหรับข้อความที่ป้อน ต่อมาแอปของคุณ ใช้คีย์นั้นเพื่อดึงข้อความของอินพุตKotlin
// Key for the string that's delivered in the action's intent. private val KEY_TEXT_REPLY = "key_text_reply" var replyLabel: String = resources.getString(R.string.reply_label) var remoteInput: RemoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).run { setLabel(replyLabel) build() }
Java
// Key for the string that's delivered in the action's intent. private static final String KEY_TEXT_REPLY = "key_text_reply"; String replyLabel = getResources().getString(R.string.reply_label); RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY) .setLabel(replyLabel) .build();
- สร้าง
PendingIntent
สำหรับการดำเนินการตอบกลับKotlin
// Build a PendingIntent for the reply action to trigger. var replyPendingIntent: PendingIntent = PendingIntent.getBroadcast(applicationContext, conversation.getConversationId(), getMessageReplyIntent(conversation.getConversationId()), PendingIntent.FLAG_UPDATE_CURRENT)
Java
// Build a PendingIntent for the reply action to trigger. PendingIntent replyPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), conversation.getConversationId(), getMessageReplyIntent(conversation.getConversationId()), PendingIntent.FLAG_UPDATE_CURRENT);
- แนบออบเจ็กต์
RemoteInput
กับการดำเนินการโดยใช้addRemoteInput()
Kotlin
// Create the reply action and add the remote input. var action: NotificationCompat.Action = NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, getString(R.string.label), replyPendingIntent) .addRemoteInput(remoteInput) .build()
Java
// Create the reply action and add the remote input. NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, getString(R.string.label), replyPendingIntent) .addRemoteInput(remoteInput) .build();
- ใช้การดำเนินการกับการแจ้งเตือนและออกการแจ้งเตือน
Kotlin
// Build the notification and add the action. val newMessageNotification = Notification.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_message) .setContentTitle(getString(R.string.title)) .setContentText(getString(R.string.content)) .addAction(action) .build() // Issue the notification. with(NotificationManagerCompat.from(this)) { notificationManager.notify(notificationId, newMessageNotification) }
Java
// Build the notification and add the action. Notification newMessageNotification = new Notification.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_message) .setContentTitle(getString(R.string.title)) .setContentText(getString(R.string.content)) .addAction(action) .build(); // Issue the notification. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, newMessageNotification);
ระบบจะแจ้งให้ผู้ใช้ป้อนคำตอบเมื่อทริกเกอร์ การดำเนินการแจ้งเตือน ดังที่แสดงในรูปที่ 4
ดึงข้อมูลที่ผู้ใช้ป้อนจากการตอบกลับ
หากต้องการรับข้อมูลที่ผู้ใช้ป้อนจาก UI การตอบกลับของการแจ้งเตือน ให้เรียกใช้
RemoteInput.getResultsFromIntent()
โดยส่ง Intent
ที่ได้รับจาก BroadcastReceiver
ของคุณ
Kotlin
private fun getMessageText(intent: Intent): CharSequence? { return RemoteInput.getResultsFromIntent(intent)?.getCharSequence(KEY_TEXT_REPLY) }
Java
private CharSequence getMessageText(Intent intent) { Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); if (remoteInput != null) { return remoteInput.getCharSequence(KEY_TEXT_REPLY); } return null; }
หลังจากประมวลผลข้อความแล้ว ให้อัปเดตการแจ้งเตือนโดยเรียกใช้
NotificationManagerCompat.notify()
ด้วยรหัสและแท็กเดียวกัน หากมีการใช้ ซึ่งจำเป็นต่อการซ่อน UI การตอบกลับโดยตรงและยืนยันกับผู้ใช้ว่าเราได้รับ
การตอบกลับและประมวลผลอย่างถูกต้องแล้ว
Kotlin
// Build a new notification, which informs the user that the system // handled their interaction with the previous notification. val repliedNotification = Notification.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_message) .setContentText(getString(R.string.replied)) .build() // Issue the new notification. NotificationManagerCompat.from(this).apply { notificationManager.notify(notificationId, repliedNotification) }
Java
// Build a new notification, which informs the user that the system // handled their interaction with the previous notification. Notification repliedNotification = new Notification.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_message) .setContentText(getString(R.string.replied)) .build(); // Issue the new notification. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, repliedNotification);
เมื่อทำงานกับการแจ้งเตือนใหม่นี้ ให้ใช้บริบทที่ส่งไปยังเมธอด
onReceive()
ของ
ผู้รับ
ต่อท้ายการตอบกลับที่ด้านล่างของการแจ้งเตือนโดยเรียกใช้
setRemoteInputHistory()
แต่หากคุณกำลังสร้างแอปรับส่งข้อความ ให้สร้างการแจ้งเตือนสไตล์การรับส่งข้อความและต่อท้ายข้อความใหม่
ในการสนทนา
ดูคำแนะนำเพิ่มเติมสำหรับการแจ้งเตือนจากแอปรับส่งข้อความได้ที่ส่วนเกี่ยวกับแนวทางปฏิบัติแนะนำสำหรับแอปรับส่งข้อความ
แสดงข้อความด่วน
แอปอาจต้องแสดงข้อความเร่งด่วนที่คำนึงถึงเวลา เช่น สายโทรศัพท์เรียกเข้าหรือเสียงปลุก ในสถานการณ์เช่นนี้ คุณสามารถเชื่อมโยง Intent แบบเต็มหน้าจอกับการแจ้งเตือนได้
เมื่อมีการเรียกใช้การแจ้งเตือน ผู้ใช้จะเห็นข้อความต่อไปนี้ข้อความใดข้อความหนึ่ง ทั้งนี้ขึ้นอยู่กับ สถานะการล็อกของอุปกรณ์
- หากอุปกรณ์ของผู้ใช้ล็อกอยู่ กิจกรรมแบบเต็มหน้าจอจะปรากฏขึ้นและครอบคลุมหน้าจอล็อก
- หากอุปกรณ์ของผู้ใช้ปลดล็อกอยู่ การแจ้งเตือนจะปรากฏในรูปแบบที่ขยาย ซึ่งมีตัวเลือกในการจัดการหรือปิดการแจ้งเตือน
ข้อมูลโค้ดต่อไปนี้แสดงวิธีเชื่อมโยงการแจ้งเตือนกับ Intent แบบเต็มหน้าจอ
Kotlin
val fullScreenIntent = Intent(this, ImportantActivity::class.java) val fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT) var builder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setFullScreenIntent(fullScreenPendingIntent, true)
Java
Intent fullScreenIntent = new Intent(this, ImportantActivity.class); PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setFullScreenIntent(fullScreenPendingIntent, true);
ตั้งค่าระดับการมองเห็นของหน้าจอล็อก
หากต้องการควบคุมระดับรายละเอียดที่มองเห็นได้ในการแจ้งเตือนจากหน้าจอล็อก ให้เรียกใช้
setVisibility()
และระบุค่าใดค่าหนึ่งต่อไปนี้
VISIBILITY_PUBLIC
: เนื้อหาทั้งหมดของการแจ้งเตือนจะแสดงในหน้าจอล็อกVISIBILITY_SECRET
: ไม่มีส่วนใดของการแจ้งเตือนแสดงในหน้าจอล็อกVISIBILITY_PRIVATE
: เฉพาะข้อมูลพื้นฐาน เช่น ไอคอนของการแจ้งเตือนและเนื้อหา ชื่อ จะแสดงในหน้าจอล็อก เนื้อหาทั้งหมดของการแจ้งเตือนไม่แสดง
เมื่อตั้งค่าเป็น VISIBILITY_PRIVATE
คุณยังระบุเนื้อหาการแจ้งเตือนเวอร์ชันอื่นที่ซ่อนรายละเอียดบางอย่างได้ด้วย เช่น แอป SMS
อาจแสดงการแจ้งเตือนที่ระบุว่า "คุณมีข้อความใหม่ 3 ข้อความ" แต่
ซ่อนเนื้อหาและผู้ส่งข้อความ หากต้องการส่งการแจ้งเตือนทางเลือกนี้ ให้สร้างการแจ้งเตือนทางเลือกด้วย NotificationCompat.Builder
ตามปกติก่อน จากนั้นแนบการแจ้งเตือนทางเลือก
กับการแจ้งเตือนปกติด้วย
setPublicVersion()
โปรดทราบว่าผู้ใช้มีสิทธิ์ควบคุมสูงสุดเสมอว่าจะให้การแจ้งเตือนแสดงในหน้าจอล็อกหรือไม่ และสามารถควบคุมการแจ้งเตือนตามช่องการแจ้งเตือนของแอปได้
อัปเดตการแจ้งเตือน
หากต้องการอัปเดตการแจ้งเตือนหลังจากออกแล้ว ให้เรียกใช้ NotificationManagerCompat.notify()
อีกครั้งโดยส่งรหัสเดียวกันกับที่คุณใช้ก่อนหน้านี้ หากคุณปิดการแจ้งเตือนก่อนหน้านี้ ระบบจะสร้างการแจ้งเตือนใหม่แทน
คุณสามารถเลือกเรียกใช้
setOnlyAlertOnce()
เพื่อให้การแจ้งเตือนขัดจังหวะผู้ใช้ด้วยเสียง การสั่น หรือสัญญาณ
ภาพเฉพาะครั้งแรกที่การแจ้งเตือนปรากฏขึ้น และไม่ใช่สำหรับการอัปเดตในภายหลัง
นำการแจ้งเตือนออก
การแจ้งเตือนจะยังคงปรากฏให้เห็นจนกว่าจะมีเหตุการณ์ต่อไปนี้เกิดขึ้น
- ผู้ใช้ปิดการแจ้งเตือน
- ผู้ใช้แตะการแจ้งเตือน หากคุณเรียกใช้
setAutoCancel()
เมื่อ สร้างการแจ้งเตือน - คุณเรียกใช้
cancel()
สำหรับรหัสการแจ้งเตือนที่เฉพาะเจาะจง วิธีนี้จะลบการแจ้งเตือนที่กำลังดำเนินการด้วย - คุณเรียกใช้
cancelAll()
ซึ่งจะนำการแจ้งเตือนทั้งหมดที่คุณออกก่อนหน้านี้ออก - ระยะเวลาที่ระบุจะหมดลง หากคุณตั้งค่าการหมดเวลาเมื่อสร้างการแจ้งเตือนโดยใช้
setTimeoutAfter()
หากจำเป็น คุณสามารถยกเลิกการแจ้งเตือนก่อนที่ระยะหมดเวลาที่ระบุจะผ่านไปได้
แนวทางปฏิบัติแนะนำสำหรับแอปรับส่งข้อความ
โปรดพิจารณาแนวทางปฏิบัติแนะนำที่ระบุไว้ที่นี่เมื่อสร้างการแจ้งเตือนสำหรับ แอปการรับส่งข้อความและแชท
ใช้ MessagingStyle
ตั้งแต่ Android 7.0 (API ระดับ 24) เป็นต้นไป Android จะมีเทมเพลตรูปแบบการแจ้งเตือนสำหรับเนื้อหาการรับส่งข้อความโดยเฉพาะ การใช้คลาส
NotificationCompat.MessagingStyle
คุณจะเปลี่ยนป้ายกำกับหลายรายการที่แสดงในการแจ้งเตือนได้
ซึ่งรวมถึงชื่อการสนทนา ข้อความเพิ่มเติม และมุมมองเนื้อหาสำหรับการแจ้งเตือน
ข้อมูลโค้ดต่อไปนี้แสดงวิธีปรับแต่งรูปแบบการแจ้งเตือน
โดยใช้คลาส MessagingStyle
Kotlin
val user = Person.Builder() .setIcon(userIcon) .setName(userName) .build() val notification = NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("2 new messages with $sender") .setContentText(subject) .setSmallIcon(R.drawable.new_message) .setStyle(NotificationCompat.MessagingStyle(user) .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson()) .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson()) ) .build()
Java
Person user = new Person.Builder() .setIcon(userIcon) .setName(userName) .build(); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("2 new messages with " + sender) .setContentText(subject) .setSmallIcon(R.drawable.new_message) .setStyle(new NotificationCompat.MessagingStyle(user) .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson()) .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson()) ) .build();
ตั้งแต่ Android 9.0 (API ระดับ 28) เป็นต้นไป คุณจะต้องใช้คลาส
Person
เพื่อให้การแสดงผลการแจ้งเตือนและอวตารเป็นไปอย่างเหมาะสม
เมื่อใช้ NotificationCompat.MessagingStyle
ให้ทำดังนี้
- โทร
MessagingStyle.setConversationTitle()
เพื่อตั้งชื่อแชทกลุ่มที่มีผู้เข้าร่วมมากกว่า 2 คน ชื่อการสนทนาที่ดีอาจเป็นชื่อของแชทกลุ่ม หรือหากไม่มีชื่อ ก็อาจเป็นรายชื่อผู้เข้าร่วมในการสนทนา หากไม่มีข้อมูลนี้ ระบบอาจเข้าใจผิดว่าข้อความดังกล่าวเป็นการสนทนาแบบตัวต่อตัวกับ ผู้ส่งข้อความล่าสุดในการสนทนา - ใช้วิธี
MessagingStyle.setData()
เพื่อรวมข้อความสื่อ เช่น รูปภาพ รองรับประเภท MIME ของรูปแบบ image/*
ใช้การตอบกลับโดยตรง
การตอบกลับโดยตรงช่วยให้ผู้ใช้ตอบกลับข้อความในบรรทัดได้
- หลังจากที่ผู้ใช้ตอบกลับด้วยการดำเนินการตอบกลับแบบในหน้า ให้ใช้
MessagingStyle.addMessage()
เพื่ออัปเดตการแจ้งเตือนMessagingStyle
และอย่าเพิกถอนหรือยกเลิกการแจ้งเตือน การไม่ยกเลิกการแจ้งเตือนจะช่วยให้ผู้ใช้ส่งคำตอบหลายรายการจากการแจ้งเตือนได้ - หากต้องการทำให้การดำเนินการตอบกลับในบรรทัดใช้งานร่วมกับ Wear OS ได้ ให้เรียกใช้
Action.WearableExtender.setHintDisplayInlineAction(true)
- ใช้วิธี
addHistoricMessage()
เพื่อระบุบริบทในการสนทนาตอบกลับโดยตรงด้วยการเพิ่มข้อความ เก่าลงในการแจ้งเตือน
เปิดใช้ฟีเจอร์ช่วยตอบ
- หากต้องการเปิดใช้ฟีเจอร์ช่วยตอบ ให้เรียกใช้
setAllowGeneratedResponses(true)
ในการดำเนินการตอบกลับ ซึ่งจะทำให้ผู้ใช้เห็นคำตอบของฟีเจอร์ช่วยตอบเมื่อมีการเชื่อมต่อการแจ้งเตือนกับอุปกรณ์ Wear OS การตอบกลับด้วยสมาร์ทรีพลาย สร้างขึ้นจากโมเดลแมชชีนเลิร์นนิงบนนาฬิกาโดยสมบูรณ์โดยใช้ บริบทที่ได้รับจากNotificationCompat.MessagingStyle
การแจ้งเตือน และไม่มีการอัปโหลดข้อมูลไปยังอินเทอร์เน็ตเพื่อสร้าง การตอบกลับ
เพิ่มข้อมูลเมตาของการแจ้งเตือน
- กำหนดข้อมูลเมตาของการแจ้งเตือนเพื่อบอกระบบวิธีจัดการการแจ้งเตือนของแอป
เมื่ออุปกรณ์อยู่ใน
Do Not Disturb mode
เช่น ใช้วิธีaddPerson()
หรือsetCategory(Notification.CATEGORY_MESSAGE)
เพื่อลบล้างโหมดห้ามรบกวน