Android: programmatically adding buttons to notification

BrickT

I am trying programmatically add Imagebuttons to notification, but I cannot find work method for this. I know this is possible, because I saw similar app

This is how I build notification:

public class MyNotification extends Notification {
private Context ctx;

public Context getCtx() {
    return ctx;
}

private NotificationManager mNotificationManager;


public MyNotification(Context ctx, int layout_id) {
    super();
    this.ctx = ctx;
    String ns = Context.NOTIFICATION_SERVICE;

    mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
    CharSequence tickerText = "Shortcuts";
    long when = System.currentTimeMillis();

    Notification.Builder builder = new Notification.Builder(ctx);
    Notification notification = builder.getNotification();
    notification.when = when;
    notification.tickerText = tickerText;
    notification.icon = R.drawable.ic_launcher;



    RemoteViews contentView = new RemoteViews(ctx.getPackageName(), layout_id);



    //set button listners
    setListeners(contentView);

    notification.contentView = contentView;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    mNotificationManager.notify(1387, notification);

}

And how i try to add ImageButton

  RemoteViews button = new RemoteViews(ctx.getPackageName(), R.layout.image_btn_layout_test);


    Intent actionIntent = new Intent("MyIntent");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, actionIntent, 0);
    button.setOnClickPendingIntent(R.id.image, pendingIntent);

    contentView.addView(R.layout.noti_layout, button);
Konstantin Berkov

Read through these questions:

How to add button to notifications in android?
Adding button action in custom notification
Handling buttons inside android notifications

Also take look at the Notification Actions developers guide.

Also it looks like after a notification is created you cannot add actions, so you should create new notification with specified actions, and then replace previous one (assign id to your notifications).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android Adding Buttons to Toolbar Programmatically

Android: Adding multiple buttons programmatically bug

Adding Buttons to notification

Adding onClick to buttons that are created programmatically

Adding buttons to toolbar programmatically in swift

Calling API and adding buttons programmatically

Buttons in android push notification

Custom notification for android with buttons

Android adding Action Buttons

Android: Programmatically adding TextInputLayout

Programmatically adding a grid of buttons inside Relative layout

Onclick listener for Notification buttons in android

Cancel (and hide) Android notification programmatically

Margins for buttons are not setting in Android programmatically

Creating multiple buttons programmatically: Android

Adding custom radio buttons in android

Adding Android buttons to view dynamically

Android adding button in FrameLayout programmatically

Android: adding programmatically a TextView is not working

Programmatically adding fragment to framelayout in android

How to add Buttons to a push notification in Android

Android Notification bar buttons become not responsive

Show android notification action buttons expanded by default

Push notification with buttons appcelerator (Android and iOS)

How to increase the ringer & notification volume programmatically in android

Android: Take screenshot of Notification bar Programmatically

How to stop playing notification sound programmatically on Android

How to fix programmatically round corners buttons on android?

Android Xamarin: Add programmatically Radio Buttons with Color