我研究了这个,发现它addAction (int icon, CharSequence title, PendingIntent intent)已被弃用,所以我用过addAction (Notification.Action action).在这两种情况下,都无法看到图标.
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_share, "", pendingIntent).build();
notificationBuilder.addAction(action);
Run Code Online (Sandbox Code Playgroud)
文本似乎工作,但我把它留空了,因此在主图像下面有一个空的空间,其中应该显示图标
随着Android 26(O)推出通知渠道,我一直在调查谷歌提供的 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);
}
Run Code Online (Sandbox Code Playgroud)
和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, …Run Code Online (Sandbox Code Playgroud)