AndroidでのNotificationをユーザーに取り消し可能にする方法
アプリの状態によって、Notificationをユーザーに取り消し可能状態と取り消し禁止状態とで偏移させたいのですが、取り消すことの出来ない状態になってしまいます。
PlayItem playingItem = state.getCurrentPlayItem();
// Notification
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentIntent(pi);
builder.setDefaults(0);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setContentTitle(playingItem.getTitle());
builder.setContentText(playingItem.getAuthor());
builder.setWhen(0);
builder.setSmallIcon(R.drawable.ic_notifiy_small);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_notifiy_large));
if (state.isPreparing() || state.isPlaying()) {
builder.addAction(R.drawable.ic_av_pause, getString(R.string.player_pause),
PendingIntent.getService(getApplicationContext(), 0, new Intent(ACTION_PAUSE), PendingIntent.FLAG_UPDATE_CURRENT));
} else {
builder.addAction(R.drawable.ic_av_play_arrow, getString(R.string.player_pause),
PendingIntent.getService(getApplicationContext(), 0, new Intent(ACTION_PLAY), PendingIntent.FLAG_UPDATE_CURRENT));
}
builder.addAction(R.drawable.ic_av_skip_next, getString(R.string.player_next),
PendingIntent.getService(getApplicationContext(), 0, new Intent(ACTION_SKIP), PendingIntent.FLAG_UPDATE_CURRENT));
builder.addAction(R.drawable.ic_player_stiop, getString(R.string.player_stop),
PendingIntent.getService(getApplicationContext(), 0, new Intent(ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT));
if (state.isPreparing() || state.isPlaying()) {
// Notificationを作成して通知
Notification notify = builder.build();
NotificationManagerCompat.from(this).notify(NOTIFICATION_PLAYER_ID, notify);
startForeground(NOTIFICATION_PLAYER_ID, notify);
} else {
//ここで通知をユーザーに削除可能な状態としたい
stopForeground(false);
builder.setAutoCancel(true);
Notification notify = builder.build();
notify.flags |= NotificationCompat.FLAG_AUTO_CANCEL;
NotificationManagerCompat.from(this).notify(NOTIFICATION_PLAYER_ID, notify);
}