Avec Android 26 (O) l'introduction des canaux de notification j'enquêtait sur Google fourni com.example.android.notificationchannels
Android 26 (O) Notification n'affiche pas d'action Icône
Cet exemple fonctionne comme prévu jusqu'à ce que je tenté d'ajouter une Action
au Notification secondaire définie dans l'exemple d'application.
Mon code ressemble à ceci: -
/**
* 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);
}
et 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;
}
L'action est affiché et fonctionne comme on le souhaite, mais il n'y a pas d'icône affichée à côté du titre d'action .
Qu'ai-je fait de mal?
Mon fichier build.gradle est indiqué ci-dessous: -
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']
}
}
Les détails Android Studio sont: -
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
Je pensais que les icônes d'action de notification ne montrent pas depuis Nougat, seul le texte. Outre la notification de style de média qui n'affiche que des icônes. –
Parce que le même constructeur est utilisé sur toutes les plates-formes. Le même objet Notification est utilisé pour rendre la notification effective sur les plates-formes antérieures à Nougat et sur les plates-formes, les actions * ont * des icônes. –
Et de nombreuses questions peuvent être évitées entièrement par une recherche appropriée. https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html À l'avenir, si vous sentez que vous avez et une question intéressante et avez trouvé une réponse après une recherche appropriée, partagez à la fois sur pile débordement (vous pouvez répondre à vos propres questions). –