【问题标题】:NotificationCompat - how to add action without icon?NotificationCompat - 如何添加没有图标的操作?
【发布时间】:2018-04-17 06:21:41
【问题描述】:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.schedule)
                .addAction(R.drawable.icon,"action test",pi)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                .setContentTitle(title)
                .setContentText(body);

以上代码创建通知并向其添加一个操作(按钮)。我希望我的按钮不显示图标,但我不知道该怎么做,因为参数addAction 中的icon 是必需的,并且不能为空。

甚至可以在没有任何图标的情况下向通知添加操作按钮(顺便说一句,操作按钮上的图标似乎甚至没有显示在牛轧糖和奥利奥上)。

【问题讨论】:

    标签: java android notifications icons action


    【解决方案1】:

    改用 NotificationCompat.Action。并将图标的值设置为0

    NotificationCompat.Action action =
                new NotificationCompat.Action.Builder(
                        0, "action test", pi
                ).build();
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.schedule)
                .addAction(action)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                .setContentTitle(title)
                .setContentText(body);
    

    在我测试过的所有设备上都工作过

    【讨论】:

    • 我猜addAction(0,"action test",pi) 应该是一样的,看看它内部调用addAction(new Action(icon, title, intent)) 的源代码,但我不确定我是否可以将0 作为资源ID 传递
    • 奇怪的是传递 0 而不是 null,但是没关系
    • int 不能为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多