ขยายการแจ้งเตือนการรับส่งข้อความไปยัง Android Auto

description: Extend messaging notifications to Android Auto using the MessagingStyle API. keywords_public: Android Auto,messaging notifications,MessagingStyle,NotificationCompat.Action,communication apps,car development,IntentService,RemoteInput,PendingIntent

แอปรับส่งข้อความที่รองรับสามารถขยายการแจ้งเตือนการรับส่งข้อความเพื่อให้ Android Auto ใช้การแจ้งเตือนเหล่านั้นได้เมื่อทำงาน Android Auto จะแสดงการแจ้งเตือนเหล่านี้และช่วยให้ผู้ใช้อ่านและตอบกลับข้อความในอินเทอร์เฟซที่สอดคล้องกันและรบกวนน้อย และเมื่อคุณใช้ MessagingStyle API คุณจะได้รับการแจ้งเตือนข้อความที่ปรับให้เหมาะสมสำหรับอุปกรณ์ Android ทั้งหมด รวมถึง Android Auto การเพิ่มประสิทธิภาพรวมถึง UI ที่ออกแบบมาโดยเฉพาะสำหรับการแจ้งเตือนข้อความ ภาพเคลื่อนไหวที่ปรับปรุงแล้ว และการรองรับรูปภาพในบรรทัด

คู่มือนี้จะแสดงวิธีขยายแอปรับส่งข้อความที่แสดงข้อความแก่ผู้ใช้และรับการตอบกลับจากผู้ใช้ เช่น แอปรับส่งข้อความ ให้ส่งการแสดงข้อความและการรับการตอบกลับไปยัง Android Auto การผสานรวมนี้จะช่วยให้ผู้ใช้เห็นประวัติข้อความจากการแจ้งเตือนที่ได้รับระหว่างเซสชัน Android Auto ที่ใช้งานอยู่เท่านั้น หากต้องการแสดงข้อความจากก่อนที่เซสชัน Android Auto ที่ใช้งานอยู่ จะเริ่มขึ้น คุณสามารถสร้างประสบการณ์การรับส่งข้อความแบบเทมเพลตได้

ดูคำแนะนำในการออกแบบที่เกี่ยวข้องได้ที่ แอปรับส่งข้อความในฮับ Design for Cars

เริ่มต้นใช้งาน

หากต้องการให้บริการรับส่งข้อความสำหรับ Android Auto แอปของคุณต้องประกาศการรองรับ Android Auto ในไฟล์ Manifest และสามารถทำสิ่งต่อไปนี้ได้

  • ประกาศการรองรับ Android Auto
  • สร้างและส่งออบเจ็กต์ NotificationCompat.MessagingStyle ที่มี ออบเจ็กต์ Action สำหรับการตอบกลับและการทำเครื่องหมายว่าอ่านแล้ว
  • จัดการการตอบกลับและการทำเครื่องหมายบทสนทนาว่าอ่านแล้วด้วย Service

แนวคิดและออบเจ็กต์

ก่อนเริ่มออกแบบแอป คุณควรทำความเข้าใจวิธีที่ Android Auto จัดการการรับส่งข้อความ

การสื่อสารแต่ละส่วนเรียกว่า ข้อความ และแสดงด้วย คลาส MessagingStyle.Message ข้อความประกอบด้วยผู้ส่ง เนื้อหาข้อความ และเวลาที่ส่งข้อความ

การสื่อสารระหว่างผู้ใช้เรียกว่า บทสนทนา และแสดงด้วยออบเจ็กต์ MessagingStyle บทสนทนาหรือ MessagingStyle ประกอบด้วยชื่อ ข้อความ และระบุว่าบทสนทนาเป็นการสื่อสารระหว่างผู้ใช้กลุ่มหนึ่งหรือไม่

หากต้องการแจ้งให้ผู้ใช้ทราบเกี่ยวกับการอัปเดตบทสนทนา เช่น ข้อความใหม่ แอปรับส่งข้อความจะโพสต์ Notification ไปยังระบบ Android Notification นี้ใช้ออบเจ็กต์ MessagingStyle เพื่อแสดง UI เฉพาะสำหรับการรับส่งข้อความในแผงการแจ้งเตือน แพลตฟอร์ม Android ยังส่ง Notification นี้ไปยัง Android Auto และระบบจะแยก MessagingStyle ออกมาเพื่อใช้โพสต์การแจ้งเตือนในจอแสดงผลของรถ

นอกจากนี้ Android Auto ยังกำหนดให้แอปต้องเพิ่ม Action ลงใน Notification เพื่อให้ผู้ใช้ตอบกลับข้อความหรือทำเครื่องหมายข้อความว่าอ่านแล้วได้โดยตรงจาก จอแสดงผลของรถ

โดยสรุป บทสนทนาเดียวจะแสดงด้วยออบเจ็กต์ Notification ที่จัดรูปแบบด้วยออบเจ็กต์ MessagingStyle MessagingStyle มีข้อความทั้งหมดภายในบทสนทนานั้นในออบเจ็กต์ MessagingStyle.Message อย่างน้อย 1 รายการ และแอปต้องแนบออบเจ็กต์ Action สำหรับการตอบกลับและการทำเครื่องหมายว่าอ่านแล้วลงใน Notification เพื่อให้เป็นไปตามข้อกำหนดของ Android Auto

ขั้นตอนการรับส่งข้อความ

ส่วนนี้อธิบายขั้นตอนการรับส่งข้อความทั่วไประหว่างแอปของคุณกับ Android Auto

  1. แอปของคุณได้รับข้อความ
  2. แอปของคุณสร้างการแจ้งเตือน MessagingStyle ที่มีออบเจ็กต์ Action สำหรับการตอบกลับและการทำเครื่องหมายว่าอ่านแล้ว
  3. Android Auto ได้รับเหตุการณ์ "การแจ้งเตือนใหม่" จากระบบ Android และค้นหา MessagingStyle, Action สำหรับการตอบกลับ และ Action สำหรับการทำเครื่องหมายว่าอ่านแล้ว
  4. Android Auto สร้างและแสดงการแจ้งเตือนในรถ
  5. หากผู้ใช้แตะการแจ้งเตือนในจอแสดงผลของรถ Android Auto จะทริกเกอร์ Action สำหรับการทำเครื่องหมายว่าอ่านแล้ว
    • แอปของคุณต้องจัดการเหตุการณ์การทำเครื่องหมายว่าอ่านแล้วนี้ในเบื้องหลัง
  6. หากผู้ใช้ตอบกลับการแจ้งเตือนโดยใช้เสียง Android Auto จะใส่คำพูดที่ถอดเสียงแล้วของผู้ใช้ลงใน Action สำหรับการตอบกลับ แล้วทริกเกอร์ Action นั้น
    • แอปของคุณต้องจัดการเหตุการณ์การตอบกลับนี้ในเบื้องหลัง

สมมติฐานเบื้องต้น

หน้านี้ไม่ได้แนะนำวิธีสร้างแอปรับส่งข้อความทั้งหมด ตัวอย่างโค้ดต่อไปนี้แสดงบางสิ่งที่แอปของคุณต้องมีก่อนที่จะเริ่มรองรับการรับส่งข้อความด้วย Android Auto

data class YourAppConversation(
        val id: Int,
        val title: String,
        val recipients: MutableList<YourAppUser>,
        val icon: Bitmap) {
    companion object {
        /** Fetches [YourAppConversation] by its [id]. */
        fun getById(id: Int): YourAppConversation = // ...
    }

    /** Replies to this conversation with the given [message]. */
    fun reply(message: String) {}

    /** Marks this conversation as read. */
    fun markAsRead() {}

    /** Retrieves all unread messages from this conversation. */
    fun getUnreadMessages(): List<YourAppMessage> { return /* ... */ }
}
data class YourAppUser(val id: Int, val name: String, val icon: Uri)
data class YourAppMessage(
    val id: Int,
    val sender: YourAppUser,
    val body: String,
    val timeReceived: Long)

ประกาศการรองรับ Android Auto

เมื่อ Android Auto ได้รับการแจ้งเตือนจากแอปรับส่งข้อความ ระบบจะตรวจสอบว่าแอปได้ประกาศการรองรับ Android Auto แล้ว หากต้องการเปิดใช้การรองรับนี้ ให้ใส่รายการต่อไปนี้ในไฟล์ Manifest ของแอป

<application>
    ...
    <meta-data
        android:name="com.google.android.gms.car.application"
        android:resource="@xml/automotive_app_desc"/>
    ...
</application>

รายการไฟล์ Manifest นี้อ้างอิงไฟล์ XML อีกไฟล์หนึ่งชื่อ automotive_app_desc.xml ซึ่ง คุณต้องสร้างในไดเรกทอรี res/xml ของโมดูลแอป ใน automotive_app_desc.xml ให้ประกาศความสามารถของ Android Auto ที่แอปของคุณรองรับ หากต้องการประกาศการรองรับการแจ้งเตือน ให้ใส่รายการต่อไปนี้

<automotiveApp>
    <uses name="notification" />
</automotiveApp>

หากตั้งค่าแอปของคุณเป็นตัวจัดการ SMS เริ่มต้นได้ ให้ใส่องค์ประกอบ <uses> ต่อไปนี้ หากไม่ใส่ Android Auto จะใช้ตัวจัดการเริ่มต้นในตัวเพื่อจัดการข้อความ SMS/MMS ขาเข้าเมื่อตั้งค่าแอปของคุณเป็นตัวจัดการ SMS เริ่มต้น ซึ่งอาจทำให้เกิดการแจ้งเตือนซ้ำ

<automotiveApp>
    ...
    <uses name="sms" />
</automotiveApp>

นำเข้าไลบรารีหลักของ AndroidX

การสร้างการแจ้งเตือนเพื่อใช้กับ Android Auto ต้องใช้ไลบรารีหลักของ AndroidX นำเข้าไลบรารีลงในโปรเจ็กต์โดยทำดังนี้

  1. ในไฟล์ build.gradle ระดับบนสุด ให้ใส่การอ้างอิงไปยังที่เก็บ Maven ของ Google ดังที่แสดงในตัวอย่างต่อไปนี้

ดึงดูด

allprojects {
    repositories {
        google()
    }
}

Kotlin

allprojects {
    repositories {
        google()
    }
}
  1. ในไฟล์ build.gradle ของโมดูลแอป ให้ใส่การอ้างอิงไลบรารี AndroidX Core ดังที่แสดงในตัวอย่างต่อไปนี้

ดึงดูด

dependencies {
    // If your app is written in Java
    implementation 'androidx.core:core:1.19.0'

    // If your app is written in Kotlin
    implementation 'androidx.core:core-ktx:1.19.0'
}

Kotlin

dependencies {
    // If your app is written in Java
    implementation("androidx.core:core:1.19.0")

    // If your app is written in Kotlin
    implementation("androidx.core:core-ktx:1.19.0")
}

จัดการการดำเนินการของผู้ใช้

แอปรับส่งข้อความของคุณต้องมีวิธีจัดการการอัปเดตบทสนทนาผ่าน Action สำหรับ Android Auto แอปรับส่งข้อความของคุณต้องจัดการออบเจ็กต์ Action 2 ประเภท ได้แก่ การตอบกลับและการทำเครื่องหมายว่าอ่านแล้ว เราขอแนะนำให้จัดการออบเจ็กต์เหล่านี้โดยใช้ IntentService ซึ่งมีความยืดหยุ่นในการจัดการการเรียกที่อาจ ใช้เวลานาน ในเบื้องหลัง ทำให้ Main Thread ของแอปเป็นอิสระ

กำหนดการดำเนินการผ่าน Intent

การดำเนินการผ่าน Intent เป็นสตริงพื้นฐานที่คุณเลือกเพื่อระบุวัตถุประสงค์ของ Intent เนื่องจากบริการเดียวสามารถจัดการ Intent ได้หลายประเภท การกำหนดสตริงการดำเนินการหลายรายการจึงง่ายกว่าการกำหนดคอมโพเนนต์ IntentService หลายรายการ

แอปรับส่งข้อความตัวอย่างในคู่มือนี้มีการดำเนินการ 2 ประเภทที่จำเป็น ได้แก่ การตอบกลับและการทำเครื่องหมายว่าอ่านแล้ว ดังที่แสดงในตัวอย่างโค้ดต่อไปนี้

private const val ACTION_REPLY = "com.example.REPLY"
private const val ACTION_MARK_AS_READ = "com.example.MARK_AS_READ"

สร้างบริการ

หากต้องการสร้างบริการที่จัดการออบเจ็กต์ Action เหล่านี้ คุณต้องมีรหัสบทสนทนา ซึ่งเป็นโครงสร้างข้อมูลที่กำหนดโดยแอปของคุณเพื่อระบุบทสนทนา นอกจากนี้ คุณยังต้องมีคีย์อินพุตระยะไกล ซึ่งเราจะพูดถึงรายละเอียดในส่วนนี้ ตัวอย่างโค้ดต่อไปนี้สร้างบริการเพื่อจัดการการดำเนินการที่จำเป็น

private const val EXTRA_CONVERSATION_ID_KEY = "conversation_id"
private const val REMOTE_INPUT_RESULT_KEY = "reply_input"

/**
 * An [IntentService] that handles reply and mark-as-read actions for
 * [YourAppConversation]s.
 */
class MessagingService : IntentService("MessagingService") {
    override fun onHandleIntent(intent: Intent?) {
        // Fetches internal data.
        val conversationId = intent!!.getIntExtra(EXTRA_CONVERSATION_ID_KEY, -1)

        // Searches the database for that conversation.
        val conversation = YourAppConversation.getById(conversationId)

        // Handles the action that was requested in the intent. The TODOs
        // are addressed in a later section.
        when (intent.action) {
            ACTION_REPLY -> TODO()
            ACTION_MARK_AS_READ -> TODO()
        }
    }
}

หากต้องการเชื่อมโยงบริการนี้กับแอป คุณต้องลงทะเบียนบริการในไฟล์ Manifest ของแอปด้วย ดังที่แสดงในตัวอย่างต่อไปนี้

<application>
    <service android:name="com.example.MessagingService" />
    ...
</application>

สร้างและจัดการ Intent

แอปอื่นๆ รวมถึง Android Auto ไม่สามารถรับ Intent ที่ทริกเกอร์ MessagingService ได้เนื่องจากระบบส่ง Intent ไปยังแอปอื่นๆ ผ่าน PendingIntent เนื่องจากข้อจำกัดนี้ ให้สร้างRemoteInput ออบเจ็กต์เพื่อให้แอปอื่นๆ ระบุข้อความตอบกลับไปยังแอปของคุณได้ ดังที่แสดงใน ตัวอย่างต่อไปนี้

/**
 * Creates a [RemoteInput] that lets remote apps provide a response string
 * to the underlying [Intent] within a [PendingIntent].
 */
fun createReplyRemoteInput(context: Context): RemoteInput {
    // RemoteInput.Builder accepts a single parameter: the key to use to store
    // the response in.
    return RemoteInput.Builder(REMOTE_INPUT_RESULT_KEY).build()
    // Note that the RemoteInput has no knowledge of the conversation. This is
    // because the data for the RemoteInput is bound to the reply Intent using
    // static methods in the RemoteInput class.
}

/** Creates an [Intent] that handles replying to the given [appConversation]. */
fun createReplyIntent(
        context: Context, appConversation: YourAppConversation): Intent {
    // Creates the intent backed by the MessagingService.
    val intent = Intent(context, MessagingService::class.java)

    // Lets the MessagingService know this is a reply request.
    intent.action = ACTION_REPLY

    // Provides the ID of the conversation that the reply applies to.
    intent.putExtra(EXTRA_CONVERSATION_ID_KEY, appConversation.id)

    return intent
}

ใน Clause Switch ACTION_REPLY ภายใน MessagingService ให้แยกข้อมูลที่จะใส่ลงใน Intent สำหรับการตอบกลับ ดังที่แสดงในตัวอย่างต่อไปนี้

ACTION_REPLY -> {
    // Extracts reply response from the intent using the same key that the
    // RemoteInput uses.
    val results: Bundle = RemoteInput.getResultsFromIntent(intent)
    val message = results.getString(REMOTE_INPUT_RESULT_KEY)

    // This conversation object comes from the MessagingService.
    conversation.reply(message)
}

คุณจัดการ Intent สำหรับการทำเครื่องหมายว่าอ่านแล้วในลักษณะที่คล้ายกัน อย่างไรก็ตาม ไม่จำเป็นต้องใช้ RemoteInput ดังที่แสดงในตัวอย่างต่อไปนี้

/** Creates an [Intent] that handles marking the [appConversation] as read. */
fun createMarkAsReadIntent(
        context: Context, appConversation: YourAppConversation): Intent {
    val intent = Intent(context, MessagingService::class.java)
    intent.action = ACTION_MARK_AS_READ
    intent.putExtra(EXTRA_CONVERSATION_ID_KEY, appConversation.id)
    return intent
}

Clause Switch ACTION_MARK_AS_READ ภายใน MessagingService ไม่ต้องใช้ตรรกะเพิ่มเติม ดังที่แสดงในตัวอย่างต่อไปนี้

// Marking as read has no other logic.
ACTION_MARK_AS_READ -> conversation.markAsRead()

แจ้งให้ผู้ใช้ทราบเกี่ยวกับข้อความ

เมื่อจัดการการดำเนินการของบทสนทนาเสร็จแล้ว ขั้นตอนถัดไปคือการสร้างการแจ้งเตือนที่เป็นไปตามข้อกำหนดของ Android Auto

สร้างการดำเนินการ

คุณส่งออบเจ็กต์ Action ไปยังแอปอื่นๆ ได้โดยใช้ Notification เพื่อทริกเกอร์เมธอดในแอปเดิม ซึ่งเป็นวิธีที่ Android Auto ใช้ทำเครื่องหมายบทสนทนาว่าอ่านแล้วหรือตอบกลับบทสนทนา

หากต้องการสร้าง Action ให้เริ่มต้นด้วย Intent ตัวอย่างต่อไปนี้แสดงวิธีสร้าง "การตอบกลับ" โดยใช้เมธอดจากส่วนก่อนหน้า:IntentcreateReplyIntent()

fun createReplyAction(
        context: Context, appConversation: YourAppConversation): Action {
    val replyIntent: Intent = createReplyIntent(context, appConversation)
    // ...

จากนั้นห่อ Intent นี้ใน PendingIntent ซึ่งจะเตรียม PendingIntent สำหรับการใช้งานแอปภายนอก PendingIntent จะล็อกการเข้าถึง Intent ที่ห่อไว้ทั้งหมดโดย แสดงเฉพาะชุดเมธอดที่เลือกไว้ซึ่งอนุญาตให้แอปที่รับทริกเกอร์ Intent หรือรับชื่อแพ็กเกจของแอปที่ส่ง แอปภายนอกจะเข้าถึง Intent หรือข้อมูลภายในไม่ได้

    // ...
    val replyPendingIntent = PendingIntent.getService(
        context,
        createReplyId(appConversation), // Method explained later.
        replyIntent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
    // ...

โปรดทราบว่า Android Auto มีข้อกำหนด 3 ข้อสำหรับ Action สำหรับการตอบกลับก่อนที่จะตั้งค่า Action ดังกล่าว

  • ต้องตั้งค่าการดำเนินการเชิงความหมายเป็น Action.SEMANTIC_ACTION_REPLY
  • Action ต้องระบุว่าจะไม่แสดงอินเทอร์เฟซผู้ใช้เมื่อทริกเกอร์
  • Action ต้องมี RemoteInput รายการเดียว

ตัวอย่างโค้ดต่อไปนี้ตั้งค่า Action สำหรับการตอบกลับที่เป็นไปตามข้อกำหนดที่ระบุไว้ก่อนหน้านี้

    // ...
    val replyAction = Action.Builder(R.drawable.reply, "Reply", replyPendingIntent)
        // Provides context to what firing the Action does.
        .setSemanticAction(Action.SEMANTIC_ACTION_REPLY)

        // The action doesn't show any UI, as required by Android Auto.
        .setShowsUserInterface(false)

        // Don't forget the reply RemoteInput. Android Auto will use this to
        // make a system call that will add the response string into
        // the reply intent so it can be extracted by the messaging app.
        .addRemoteInput(createReplyRemoteInput(context))
        .build()

    return replyAction
}

การจัดการการดำเนินการทำเครื่องหมายว่าอ่านแล้วจะคล้ายกัน ยกเว้นว่าจะไม่มี RemoteInput ดังนั้น Android Auto จึงมีข้อกำหนด 2 ข้อสำหรับ Action สำหรับการทำเครื่องหมายว่าอ่านแล้ว

  • ตั้งค่าการดำเนินการเชิงความหมายเป็น Action.SEMANTIC_ACTION_MARK_AS_READ
  • การดำเนินการระบุว่าจะไม่แสดงอินเทอร์เฟซผู้ใช้เมื่อทริกเกอร์

ตัวอย่างโค้ดต่อไปนี้ตั้งค่า Action สำหรับการทำเครื่องหมายว่าอ่านแล้วที่เป็นไปตามข้อกำหนดเหล่านี้

fun createMarkAsReadAction(
        context: Context, appConversation: YourAppConversation): Action {
    val markAsReadIntent = createMarkAsReadIntent(context, appConversation)
    val markAsReadPendingIntent = PendingIntent.getService(
            context,
            createMarkAsReadId(appConversation), // Method explained later.
            markAsReadIntent,
            PendingIntent.FLAG_UPDATE_CURRENT  or PendingIntent.FLAG_IMMUTABLE)
    val markAsReadAction = Action.Builder(
            R.drawable.mark_as_read, "Mark as Read", markAsReadPendingIntent)
        .setSemanticAction(Action.SEMANTIC_ACTION_MARK_AS_READ)
        .setShowsUserInterface(false)
        .build()
    return markAsReadAction
}

เมื่อสร้าง PendingIntent คุณจะใช้ 2 เมธอด ได้แก่ createReplyId และ createMarkAsReadId เมธอดเหล่านี้ทำหน้าที่เป็นรหัสคำขอสำหรับ PendingIntent แต่ละรายการ ซึ่ง Android ใช้เพื่อควบคุม PendingIntent ที่มีอยู่ เมธอด create ต้อง ส่งคืนรหัสที่ไม่ซ้ำกันสำหรับบทสนทนาแต่ละรายการ แต่การเรียกซ้ำสำหรับบทสนทนาเดียวกันต้องส่งคืนรหัสที่ไม่ซ้ำกันที่สร้างขึ้นแล้ว

ลองดูตัวอย่างบทสนทนา 2 รายการ ได้แก่ A และ B โดยรหัสการตอบกลับของบทสนทนา A คือ 100 และรหัสการทำเครื่องหมายว่าอ่านแล้วคือ 101 รหัสการตอบกลับของบทสนทนา B คือ 102 และรหัสการทำเครื่องหมายว่าอ่านแล้วคือ 103 หากมีการอัปเดตบทสนทนา A รหัสการตอบกลับและรหัสการทำเครื่องหมายว่าอ่านแล้วจะยังคงเป็น 100 และ 101 ดูข้อมูลเพิ่มเติมได้ที่ PendingIntent.FLAG_UPDATE_CURRENT

สร้าง MessagingStyle

MessagingStyle เป็นตัวเก็บข้อมูลการรับส่งข้อความและเป็นสิ่งที่ Android Auto ใช้เพื่ออ่านออกเสียงข้อความแต่ละรายการในบทสนทนา

ขั้นแรก คุณต้องระบุผู้ใช้อุปกรณ์เป็นออบเจ็กต์ Person ดังที่ แสดงในตัวอย่างต่อไปนี้

fun createMessagingStyle(
        context: Context, appConversation: YourAppConversation): MessagingStyle {
    // Method defined by the messaging app.
    val appDeviceUser: YourAppUser = getAppDeviceUser()

    val devicePerson = Person.Builder()
        // The display name (also the name that's read aloud in Android auto).
        .setName(appDeviceUser.name)

        // The icon to show in the notification shade in the system UI (outside
        // of Android Auto).
        .setIcon(appDeviceUser.icon)

        // A unique key in case there are multiple people in this conversation with
        // the same name.
        .setKey(appDeviceUser.id)
        .build()
    // ...

จากนั้นสร้างออบเจ็กต์ MessagingStyle และระบุรายละเอียดบางอย่างเกี่ยวกับบทสนทนา

    // ...
    val messagingStyle = MessagingStyle(devicePerson)

    // Sets the conversation title. If the app's target version is lower
    // than P, this will automatically mark the conversation as a group (to
    // maintain backward compatibility). Use `setGroupConversation` after
    // setting the conversation title to explicitly override this behavior. See
    // the documentation for more information.
    messagingStyle.setConversationTitle(appConversation.title)

    // Group conversation means there is more than 1 recipient, so set it as such.
    messagingStyle.setGroupConversation(appConversation.recipients.size > 1)
    // ...

สุดท้าย ให้เพิ่มข้อความที่ยังไม่ได้อ่าน

    // ...
    for (appMessage in appConversation.getUnreadMessages()) {
        // The sender is also represented using a Person object.
        val senderPerson = Person.Builder()
            .setName(appMessage.sender.name)
            .setIcon(appMessage.sender.icon)
            .setKey(appMessage.sender.id)
            .build()

        // Adds the message. More complex messages, like images,
        // can be created and added by instantiating the MessagingStyle.Message
        // class directly. See documentation for details.
        messagingStyle.addMessage(
                appMessage.body, appMessage.timeReceived, senderPerson)
    }

    return messagingStyle
}

จัดแพ็กเกจและส่งการแจ้งเตือน

หลังจากสร้างออบเจ็กต์ Action และ MessagingStyle แล้ว คุณก็สร้างและโพสต์ Notification ได้

fun notify(context: Context, appConversation: YourAppConversation) {
    // Creates the actions and MessagingStyle.
    val replyAction = createReplyAction(context, appConversation)
    val markAsReadAction = createMarkAsReadAction(context, appConversation)
    val messagingStyle = createMessagingStyle(context, appConversation)

    // Creates the notification.
    val notification = NotificationCompat.Builder(context, channel)
        // A required field for the Android UI.
        .setSmallIcon(R.drawable.notification_icon)

        // Shows in Android Auto as the conversation image.
        .setLargeIcon(appConversation.icon)

        // Adds MessagingStyle.
        .setStyle(messagingStyle)

        // Adds reply action.
        .addAction(replyAction)

        // Makes the mark-as-read action invisible, so it doesn't appear
        // in the Android UI but the app satisfies Android Auto's
        // mark-as-read Action requirement. Both required actions can be made
        // visible or invisible; it is a stylistic choice.
        .addInvisibleAction(markAsReadAction)

        .build()

    // Posts the notification for the user to see.
    val notificationManagerCompat = NotificationManagerCompat.from(context)
    notificationManagerCompat.notify(appConversation.id, notification)
}

แหล่งข้อมูลเพิ่มเติม

รายงานปัญหาการแจ้งเตือนการรับส่งข้อความของ Android Auto

หากพบปัญหาขณะพัฒนาการแจ้งเตือนการรับส่งข้อความสำหรับ Android Auto คุณสามารถรายงานปัญหาได้โดยใช้ Google Issue Tracker โปรดกรอกข้อมูลที่จำเป็นทั้งหมดในเทมเพลตปัญหา

สร้างปัญหาใหม่

ก่อนยื่นเรื่องใหม่ โปรดตรวจสอบว่ามีการรายงานปัญหาดังกล่าวในรายการปัญหาแล้วหรือยัง คุณสามารถติดตามและโหวตปัญหาได้โดยคลิกดาวสำหรับปัญหาในเครื่องมือติดตาม ดูข้อมูลเพิ่มเติมได้ที่การติดตามปัญหา