Уведомление Android.addAction устарело в api 23
Каким образом можно добавить действие к уведомлению в API 23
, поскольку addAction(int icon, CharSequence title, PendingIntent intent)
устарел? Не удалось найти какой-либо пример, спасибо.
Мое старое действие: .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)
Ответы
Ответ 1
Вместо этого:
addAction (значок int, заголовок CharSequence, намерение PendingIntent)
Этот метод устарел на уровне API 23.
Использование:
addAction (действие Notification.Action)
Все это в документах разработчика!
Итак, чтобы использовать это:
сначала создайте свое действие с помощью NotificationCompat.Action.Builder
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_prev, "Previous", prevPendingIntent).build();
Примечание: Используйте NotificationCompat.Action
И добавьте его в свое уведомление:
yournotification.addAction(action);
Ответ 2
Используйте Icon
класс при первом параметре для Drawable , если api level >= 23 (marshmallow)
https://developer.android.com/reference/android/app/Notification.Action.Builder.html
https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.Action.Builder.html
пример)
Notification.Action action = new Notification.Action.Builder(
Icon.createWithResource(this, R.drawable.ic_prev),
"action string",
pendingIntent).build();
Ответ 3
//Exemple of notification with Button
private void scheduleNotificationWithButton(String message) {
int notifReCode = 1;
//What happen when you will click on button
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
//Button
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build();
//Notification
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Back to Application ?")
.setContentTitle("Amazing news")
.addAction(action) //add buton
.build();
//Send notification
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
Ответ 4
Вам просто нужно использовать Builder NotificationCompat.Builder вместо Builder Notification.Builder, поскольку NotificationCompat.Builder позволяет вам создавать приложение под Android версии 4.1
Решено с помощью NotificationCompat.builder:
String strTitle = getString(R.string.new_message);
String strText = getString(R.string.hi_whats_up);
Intent intent = new Intent(this, NotificationView.class);
intent.putExtra("title", strTitle);
intent.putExtra("text", strText);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Notification Ticker")
.setContentTitle("Notification Title")
.setContentText("Notification Text")
.addAction(R.mipmap.ic_launcher, "Notification Action", pendingIntent)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());