เพิ่มการแจ้งเตือนในแอปสื่อ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมื่อสร้างแอปสื่อที่ประมวลผลเสียงหรือวิดีโอ คุณจำเป็นต้องใช้
การแจ้งเตือนและช่องทางการแจ้งเตือนที่ถูกต้อง ช่วงเวลานี้
ตรวจสอบว่าการแจ้งเตือนมีฟีเจอร์ที่เป็นประโยชน์ดังต่อไปนี้
- มีลำดับความสำคัญของการแจ้งเตือน
- ปิดไม่ได้
- ใช้แอตทริบิวต์เสียงสำหรับเสียงเรียกเข้า
ใช้ NotificationChannel.Builder
เพื่อสร้างช่องทางการแจ้งเตือน 2 ช่องทาง โดยช่องทางหนึ่งสำหรับ
สายเรียกเข้าและอื่นๆ สำหรับสายที่สนทนาอยู่
internal companion object {
const val TELECOM_NOTIFICATION_ID = 200
const val TELECOM_NOTIFICATION_ACTION = "telecom_action"
const val TELECOM_NOTIFICATION_INCOMING_CHANNEL_ID = "telecom_incoming_channel"
const val TELECOM_NOTIFICATION_ONGOING_CHANNEL_ID = "telecom_ongoing_channel"
private val ringToneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
}
เพื่อแสดงการแจ้งเตือนในทุกที่และอนุญาตให้เล่นเสียงสำหรับ
เสียงเรียกเข้า ให้กำหนดลำดับความสำคัญของช่องการแจ้งเตือนที่เข้ามาเป็นสูง
val incomingChannel = NotificationChannelCompat.Builder(
TELECOM_NOTIFICATION_INCOMING_CHANNEL_ID,
NotificationManagerCompat.IMPORTANCE_HIGH,
).setName("Incoming calls")
.setDescription("Handles the notifications when receiving a call")
.setVibrationEnabled(true).setSound(
ringToneUri,
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setLegacyStreamType(AudioManager.STREAM_RING)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build(),
).build()
เฉพาะการโทรที่ดำเนินอยู่เท่านั้นที่ต้องตั้งค่าความสำคัญเป็นค่าเริ่มต้น ใช้เมนู
รูปแบบสายเรียกเข้าต่อไปนี้ เพื่อให้การแจ้งเตือนสายเรียกเข้า
ปิดไม่ได้
val ongoingChannel = NotificationChannelCompat.Builder(
TELECOM_NOTIFICATION_ONGOING_CHANNEL_ID,
NotificationManagerCompat.IMPORTANCE_DEFAULT,
)
.setName("Ongoing calls")
.setDescription("Displays the ongoing call notifications")
.build()
ในการจัดการกับกรณีการใช้งานที่อุปกรณ์ของผู้ใช้ถูกล็อกระหว่างสายเรียกเข้า
ใช้การแจ้งเตือนแบบเต็มหน้าจอเพื่อแสดงกิจกรรมที่อนุญาตให้ผู้ใช้
รับสาย
// on the notification
val contentIntent = PendingIntent.getActivity(
/* context = */ context,
/* requestCode = */ 0,
/* intent = */ Intent(context, TelecomCallActivity::class.java),
/* flags = */ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
อ่านสร้างการแจ้งเตือนรูปแบบการโทรสำหรับแอปเพื่อการโทรเพื่อดูวิธีการ
ใช้ CallStyle
เพื่อแยกการแจ้งเตือนสายเรียกเข้าออกจากประเภท
การแจ้งเตือน
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Add notifications to a media app\n\nWhen building a media app that processes audio or video, it's important to use\nthe correct notifications and notification channels. This\nensures that notifications have the following valuable features:\n\n- Have notification priority\n- Are non-dismissable\n- Use audio attributes for ringtones\n\nUse `NotificationChannel.Builder` to set up two notification channels: one for\nincoming calls and the other for active calls. \n\n internal companion object {\n const val TELECOM_NOTIFICATION_ID = 200\n const val TELECOM_NOTIFICATION_ACTION = \"telecom_action\"\n const val TELECOM_NOTIFICATION_INCOMING_CHANNEL_ID = \"telecom_incoming_channel\"\n const val TELECOM_NOTIFICATION_ONGOING_CHANNEL_ID = \"telecom_ongoing_channel\"\n\n private val ringToneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)\n }\n\nTo display the notification everywhere and allow it to play audio for the\nringtone, set the importance of the incoming notification channel to high. \n\n val incomingChannel = NotificationChannelCompat.Builder(\n TELECOM_NOTIFICATION_INCOMING_CHANNEL_ID,\n NotificationManagerCompat.IMPORTANCE_HIGH,\n ).setName(\"Incoming calls\")\n .setDescription(\"Handles the notifications when receiving a call\")\n .setVibrationEnabled(true).setSound(\n ringToneUri,\n AudioAttributes.Builder()\n .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)\n .setLegacyStreamType(AudioManager.STREAM_RING)\n .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build(),\n ).build()\n\nOnly active calls requires the importance to be set to default. Use the\nfollowing incoming call style to allow notifications for incoming calls to be\nnon-dismissable. \n\n val ongoingChannel = NotificationChannelCompat.Builder(\n TELECOM_NOTIFICATION_ONGOING_CHANNEL_ID,\n NotificationManagerCompat.IMPORTANCE_DEFAULT,\n )\n .setName(\"Ongoing calls\")\n .setDescription(\"Displays the ongoing call notifications\")\n .build()\n\nTo address use cases where the user's device is locked during an incoming call,\nuse a full-screen notification to display an activity to allow the user to\nanswer the call. \n\n // on the notification\n val contentIntent = PendingIntent.getActivity(\n /* context = */ context,\n /* requestCode = */ 0,\n /* intent = */ Intent(context, TelecomCallActivity::class.java),\n /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,\n )\n\nRead [Create a call style notification for call apps](/develop/ui/views/notifications/call-style) for instructions on\nusing [`CallStyle`](/reference/android/app/Notification.CallStyle) to distinguishing call notifications from other types of\nnotifications."]]