2016-02-26 2 views
10

Каков порядок добавления уведомления об уведомлении в API 23 с addAction(int icon, CharSequence title, PendingIntent intent) устарел? Не мог найти никакого примера, спасибо.Уведомление Android .addAction устарело в api 23

Мой старый действие: .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)

+0

В соответствии с [документы] (http://developer.android.com/reference/android /app/Notification.Builder.html#addAction%28android.app.Notification.Action%29), вы должны использовать 'addAction (Notification.Action action)' вместо этого. – Tigger

+0

http://stackoverflow.com/questions/16852270/how-to-implement-the-deprecated-methods-of-notification – sasikumar

+2

Я уже посмотрел на этот пример @sasikumar, это мне не помогает, я не понимаю как создать 'Notification.Action action' –

ответ

11

Вместо этого один:

addAction (int icon, CharSequence title, PendingIntent intent)

Этот метод осуждался в уровне API 23.

Использование:

addAction (Notification.Action action)

Это все в документации для разработчиков!

Так, чтобы использовать это:

первым строить свои действия с NotificationCompat.Action.Builder

NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_prev, "Previous", prevPendingIntent).build(); 

Примечание: Использование NotificationCompat.Action

И чем добавить его в уведомлении:

yournotification.addAction(action); 
+0

Я знаю, я его уже читал, но я не понимаю, как правильно построить' Notification.Action action' –

+0

Хорошо, что я отредактировал его для u – Strider

+0

Это неправильные, несовместимые типы , вам нужно изменить 'new Notification.Action.Builder()' на 'new Action()' и результат: 'new Notification.Action (.....) 'все еще устарел или наоборот, который все еще не рекомендуется. –

8

Использование Icon класс на первый параметр для Drawable если уровень апи> = 23 (зефира)

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(); 
+0

Старый вопрос, но ему все еще нужно :) собирается проверить его сегодня, и я приду с ответом, спасибо. –

2
//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); 
} 
1

Вы просто должны используйте NotificationCompat.Builder builder вместо Builder Notification.Builder, потому что Нет tificationCompat.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()); 
+0

Это отлично работает для меня. Кажется избыточным для android, чтобы иметь так много способов создать глупое уведомление? – Thunderstick

Смежные вопросы