Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android notification .addAction deprecated in api 23

Tags:

android

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)

like image 354
Marian Pavel Avatar asked Feb 26 '16 09:02

Marian Pavel


People also ask

What can I use instead of NotificationCompat builder?

This method is deprecated. use Builder(Context, String) instead. All posted notifications must specify a NotificationChannel ID.

How do I make my notification icons smaller?

Set the small icon resource, which will be used to represent the notification in the status bar. Set the small icon, which will be used to represent the notification in the status bar and content view (unless overridden there by a large icon ).


2 Answers

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);
like image 80
Strider Avatar answered Sep 28 '22 00:09

Strider


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();
like image 39
leejaycoke Avatar answered Sep 28 '22 01:09

leejaycoke