2017-06-22 48 views
2

隨着Android的26(O)引入通知渠道我一直在調查谷歌提供的com.example.android.notificationchannelsAndroid的26(O)通知不顯示操作圖標

此示例按預期工作,直到我試圖添加一個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 
+1

我以爲通知操作圖標不顯示,因爲牛軋糖,只有文本。除了只顯示圖標的媒體樣式通知。 –

+0

因爲在所有平臺上都使用相同的構造函數。相同的Notification對象用於在Nougat之前的平臺上呈現實際通知,並在這些平臺上執行* do *圖標。 –

+0

許多問題都可以通過適當的研究完全避免。 https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html在未來,如果您覺得自己有一些有趣的問題,並在正確的研究後找到答案,堆棧溢出(你可以回答自己的問題)。 –

回答

4

自Nougat以來,通知操作不會與圖標一起顯示。

enter image description here

你會注意到,該圖標都沒有出現在新的通知;而是在通知欄的受限空間中爲標籤本身提供更多空間。 但是,通知操作圖標仍然是必需的,並且會繼續在舊版Android和諸如Android Wear等設備上使用。

來源:https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html,ephasis mine。

總之,通知操作圖標是必需的,用於:

  • 在舊版本的Android,在可穿戴設備
  • 媒體風格通知。
-1

嘗試使用NotificationCompat instead.It似乎爲我工作。

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

我相信android.support.v7.app.NotificationCompat不支持通知頻道。 – Hector