11

With Android 26 (O) introducing Notification channels I have been investigating the Google supplied com.example.android.notificationchannels

This example works as expected until I attempted to add an Action to the Secondary Notification defined within the example app.

My code resembles this :-

   /**
     * 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);
    }

and 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;
    }

The Action is displayed and works as desired, however there is no Icon displayed next to the Action Title.

What have I done wrong?

My build.gradle file is shown below:-

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']

    }

}

The Android Studio details are :-

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
4

2 回答 2

32

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

在此处输入图像描述

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

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

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

  • 在旧版本的 Android 上,
  • 在可穿戴设备上,
  • 在媒体风格的通知中。
于 2017-06-23T08:52:47.513 回答
-3

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

NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action, "YOUR_ACTION", mPendingIntent).build();
于 2017-06-22T12:12:24.670 回答