【发布时间】: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