【问题标题】:Android 26 (O) Notification doesn't display Action Icon [duplicate]Android 26(O)通知不显示操作图标[重复]
【发布时间】:2017-06-22 11:42:52
【问题描述】:

随着 Android 26 (O) 引入通知渠道,我一直在调查 Google 提供的 com.example.android.notificationchannels

在我尝试将 Action 添加到示例应用中定义的辅助通知之前,此示例将按预期工作。

我的代码类似于:-

   /**
     * Build notification for secondary channel.
     *
     * @param title Title for notification.
     * @param body Message for notification.
     * @return A Notification.Builder configured with the selected channel and details
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getNotification2(String title, String body) {
        return new Notification.Builder(getApplicationContext(), SECONDARY_CHANNEL)
                .setContentTitle(title)
                .setContentText(body)
                .setActions(buildAction())
                .setSmallIcon(getSmallIcon())
                .setAutoCancel(true);
    }

和 buildAction() :-

   @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
    private Notification.Action buildAction() {

        final Intent intent = new Intent(this, SecondActivity.class);
        final PendingIntent pendingIntent =  PendingIntent.getActivity(this, 1729, intent, PendingIntent.FLAG_UPDATE_CURRENT );

        final Notification.Action myAction = new Notification.Action.Builder(R.drawable.ic_action_name, "RETRY", pendingIntent).build();

        return myAction;
    }

动作会按需要显示和工作,但是动作标题旁边没有显示图标。

我做错了什么?

我的 build.gradle 文件如下所示:-

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
    }
}

apply plugin: 'com.android.application'

repositories {
    jcenter()
}

dependencies {
    compile "com.android.support:support-v4:26.+"
    compile "com.android.support:support-v13:26.+"
    compile "com.android.support:cardview-v7:26.+"
    compile "com.android.support:appcompat-v7:26.+"
}

// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
    'main',     // main sample code; look here for the interesting stuff.
    'common',   // components that are reused by multiple samples
    'template'] // boilerplate code that is generated by the sample template process

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"

    // Values declared here override the ones declared in AndroidManifest.xml
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 26
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            dirs.each { dir ->
                java.srcDirs "src/${dir}/java"
                res.srcDirs "src/${dir}/res"
            }
        }
        androidTest.setRoot('tests')
        androidTest.java.srcDirs = ['tests/src']

    }

}

Android Studio 详情如下:-

Android Studio 3.0 Canary 4
Build #AI-171.4101728, built on June 15, 2017
JRE: 1.8.0_112-release-b736 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.11.6

【问题讨论】:

  • 我认为通知操作图标自 Nougat 以来不显示,只有文本。除了只显示图标的媒体样式通知。
  • 因为所有平台都使用相同的构造函数。相同的 Notification 对象用于在 Nougat 之前的平台上呈现实际通知,并且在这些平台上,操作确实具有图标。
  • 通过适当的研究可以完全避免许多问题。 android-developers.googleblog.com/2016/06/… 以后如果你觉得自己有有趣的问题,经过适当的研究找到了答案,在stackoverflow上分享一下(你可以自己回答问题)。
  • 那么先生,您需要拓宽视野:stackoverflow.com/questions/41503972/… stackoverflow.com/questions/42082882/… 在 I/O 中没有提到,因为这不是新闻。
  • 我不是先生。其次,您作为副本链接的问题特别显示 NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_share, "", pendingIntent).build() 我没有使用 Compat 库。

标签: android android-notifications android-8.0-oreo


【解决方案1】:

自 Nougat 以来,通知操作不显示图标。

您会注意到新通知中没有图标;相反,在通知栏的受限空间中为标签本身提供了更多空间。 但是,通知操作图标仍然是必需的,并且会继续在旧版 Android 和 Android Wear 等设备上使用。

来源:https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html,我的重点。

总而言之,通知操作图标是必需的并被使用:

  • 在旧版本的 Android 上,
  • 关于可穿戴设备,
  • 在媒体样式通知中。

【讨论】:

  • 只是为了好玩...我在标签字符串中添加了表情符号,所以至少在文本旁边有一个图形。
【解决方案2】:

尝试改用NotificationCompat。它似乎对我有用。

NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action, "YOUR_ACTION", mPendingIntent).build();

【讨论】:

  • 我认为 android.support.v7.app.NotificationCompat 不支持通知渠道。
猜你喜欢
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 2018-01-05
  • 1970-01-01
相关资源
最近更新 更多