You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Auto模拟器(DHU)无法接收应用通知的问题求助

修复Android Auto模拟器(DHU)中MessagingStyle通知不显示的问题

你的代码已使用MessagingStyle,但通知无法在DHU中显示,核心原因是缺少Android Auto所需的对话属性配置、合规的通知渠道设置,以及Person角色的正确区分。以下是具体修复方案:

1. 确保通知渠道正确创建(Android O+)

Android 8.0及以上必须创建通知渠道,且渠道优先级需设为IMPORTANCE_HIGH才能在Auto中显示。在MainActivity的onCreate方法中添加渠道创建代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 创建适配Android Auto的通知渠道
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Reminder Notifications", NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("Reminder notifications synced to Android Auto");
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    // 绑定通知触发按钮
    findViewById(R.id.btn_show_notification).setOnClickListener(v -> showNotification(this));
}

2. 修正MessagingStyle与CarExtender配置

Android Auto需要明确区分对话中的发送者和当前用户,且需通过CarExtender配置未读对话属性。修改showNotification方法如下:

private void showNotification(Context context) {
    // 定义当前用户(设备所有者)和消息发送者
    Person currentUser = new Person.Builder()
            .setName("Me")
            .build();
    Person sender = new Person.Builder()
            .setName("Reminder")
            .setIcon(Icon.createWithResource(context, android.R.drawable.ic_dialog_info))
            .build();

    // 配置MessagingStyle:构造参数为当前用户,消息由sender发起
    NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(currentUser)
            .setConversationTitle("Reminder!")
            .addMessage("Do you want to continue??", System.currentTimeMillis(), sender);

    // Yes Action(回复)- 使用FLAG_MUTABLE适配Android 12+
    Intent yesIntent = new Intent(context, MessagingService.class);
    yesIntent.setAction(MessagingService.ACTION_REPLY);
    PendingIntent yesPendingIntent = PendingIntent.getService(
            context,
            0,
            yesIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE
    );

    NotificationCompat.Action yesAction = new NotificationCompat.Action.Builder(
            android.R.drawable.ic_menu_send,
            "Yes",
            yesPendingIntent
    )
            .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
            .build();

    // No Action(标记已读)
    Intent noIntent = new Intent(context, MessagingService.class);
    noIntent.setAction(MessagingService.ACTION_MARK_AS_READ);
    PendingIntent noPendingIntent = PendingIntent.getService(
            context,
            1,
            noIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );

    NotificationCompat.Action noAction = new NotificationCompat.Action.Builder(
            android.R.drawable.ic_menu_close_clear_cancel,
            "No",
            noPendingIntent
    )
            .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
            .build();

    // 配置CarExtender:添加Android Auto所需的未读对话属性
    CarExtender carExtender = new CarExtender();
    carExtender.setUnreadConversation(
            new CarExtender.UnreadConversation.Builder(sender)
                    .setLatestTimestamp(System.currentTimeMillis())
                    .setReadPendingIntent(noPendingIntent)
                    .setReplyAction(yesPendingIntent, MessagingStyle.REPLY_CHARACTER_LIMIT_DEFAULT)
                    .build()
    );

    // 构建最终通知
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setStyle(messagingStyle)
            .addAction(yesAction)
            .addAction(noAction)
            .extend(carExtender); // 应用Android Auto扩展配置

    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
}

3. 完善Manifest配置

确保在AndroidManifest.xml中添加必要权限并注册服务:

<!-- Android 13+通知权限 -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<!-- Android Auto信息访问权限 -->
<uses-permission android:name="com.google.android.gms.permission.CAR_INFORMATION"/>

<application ...>
    <!-- 注册MessagingService -->
    <service
        android:name=".MessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.example.REPLY"/>
            <action android:name="com.example.MARK_AS_READ"/>
        </intent-filter>
    </service>

    <!-- 其他组件声明 -->
</application>

4. DHU模拟器检查项

  • 确认手机与DHU连接正常,Android Auto应用已识别到模拟器
  • 在手机「设置」→「应用和通知」→「Android Auto」中,允许你的应用显示通知
  • 检查应用是否已获得「通知权限」

内容的提问来源于stack exchange,提问作者Md101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 22:26:04