16

What is the proper way to add an action to the notification in API 23 since addAction(int icon, CharSequence title, PendingIntent intent) is deprecated ? Couldn't find any example, thank you.

My old action: .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)

Marian Pavel
  • 2,726
  • 8
  • 29
  • 65
  • According to the [docs](http://developer.android.com/reference/android/app/Notification.Builder.html#addAction%28android.app.Notification.Action%29), you should use `addAction (Notification.Action action)` instead. – Tigger Feb 26 '16 at 09:25
  • http://stackoverflow.com/questions/16852270/how-to-implement-the-deprecated-methods-of-notification – sasikumar Feb 26 '16 at 09:27
  • 3
    I already looked at that example @sasikumar, it doesn't help me, I don't understand how to build the `Notification.Action action` – Marian Pavel Feb 26 '16 at 09:31

6 Answers6

24

Instead of this one:

addAction (int icon, CharSequence title, PendingIntent intent)

This method was deprecated in API level 23.

Use:

addAction (Notification.Action action)

It's all in the developer docs!

So to use this:

first build your action with the NotificationCompat.Action.Builder

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

Note: Use NotificationCompat.Action

And than add it to your notification:

yournotification.addAction(action);
Community
  • 1
  • 1
Strider
  • 4,452
  • 3
  • 24
  • 35
16

Use Icon class at first parameter for Drawable if 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

example)

Notification.Action action = new Notification.Action.Builder(
    Icon.createWithResource(this, R.drawable.ic_prev),
    "action string",
    pendingIntent).build();
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
leejaycoke
  • 709
  • 7
  • 7
4
//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);
}
Anthone
  • 2,156
  • 21
  • 26
2

You just have to use NotificationCompat.Builder builder in place of Notification.Builder builder because NotificationCompat.Builder allows you to build your application below Android Version 4.1

Solved by using 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());
1
Actually, the only option to use all non-deprecated methods is by Using Icon for Action.Builder.

    
Notification.Action action = new Notification.Action.Builder(Icon.createWithResource(this, R.drawable.act_hike), 
getString(R.string.finish_recording), pActionFinish).build();

//Create a notification
Notification notification = new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.atlas)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.foreground_notification))
.addAction(action)
.setContentIntent(pIntent)
.build();
Tal Vaknin
  • 71
  • 1
  • 3
0

Use special builder Notification.Action.Builder.

    Notification.Action.Builder builder = new Notification.Action.Builder( Icon.createWithResource( this, R.drawable.ic_pause), getString( R.string.pause ), PendingIntent.getBroadcast( this, 1, new Intent( TIMER_PAUSE ), 0 ) );
    Notification.Action action = builder.build();
...
    notification_builder.addAction( action ); // or setActions( action );
Style-7
  • 985
  • 12
  • 27