Android 26 (O) 通知不显示操作图标
Android 26 (O) Notification doesn't display Action Icon
随着 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工作室的详细信息是:-
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
尝试使用 NotificationCompat
instead.It 似乎对我有用。
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action, "YOUR_ACTION", mPendingIntent).build();
自牛轧糖以来,通知操作不再以图标显示。
You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required and continue to be used on older versions of Android and on devices such as Android Wear.
来源:https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html,重点是我的。
总而言之,通知操作图标是必需的并且已被使用:
- 旧版本 Android、
- 在可穿戴设备上,
- 在媒体样式通知中。
随着 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工作室的详细信息是:-
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
尝试使用 NotificationCompat
instead.It 似乎对我有用。
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action, "YOUR_ACTION", mPendingIntent).build();
自牛轧糖以来,通知操作不再以图标显示。
You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required and continue to be used on older versions of Android and on devices such as Android Wear.
来源:https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html,重点是我的。
总而言之,通知操作图标是必需的并且已被使用:
- 旧版本 Android、
- 在可穿戴设备上,
- 在媒体样式通知中。