Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Buttons to a push notification in Android [closed]

Tags:

java

android

I want my push notification to have two buttons but I couldn't find anywhere how to do so. Can anyone explain how to add buttons to a Push Notification in Android?

NotificationCompat.Builder notification;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verify);
    notification=new NotificationCompat.Builder(this);
    notification.setAutoCancel(true);
    /* blah blah blah  */
}

public void onVerify(View v)
{
    /*
     some code
    */
    if(example){ /* another piece of code */ }
    else{
        String num=data.getString("Phone");
        this.defineNotification(num);
        //Log.i(TAG,"here ");
        Intent i=new Intent(this,VendorDetails.class);
        i.putExtra("Phone",num);
        startActivity(i);
    }
}
public void defineNotification(String num)
{
    notification.setContentTitle("Successful");
    notification.setContentText("Hi, I am being displayed now");
    notification.setWhen(System.currentTimeMillis());
    notification.setSmallIcon(R.mipmap.ic_launcher);
    Intent i=new Intent(this,OrderTypes.class);
    PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
    notification.addAction(R.mipmap.ic_launcher,"Accept",pi);
    notification.addAction(R.mipmap.ic_launcher, "Decline", pi);
    NotificationCompat.MediaStyle mediaStyle=new NotificationCompat.MediaStyle();
    MediaSessionCompat mediaSession=new MediaSessionCompat(this,TAG);
    mediaStyle.setShowActionsInCompactView(1);
    mediaStyle.setMediaSession(mediaSession.getSessionToken());
    notification.setStyle(mediaStyle);


    Intent intent=new Intent(this,VendorDetails.class);
    intent.putExtra("Phone",num);
    PendingIntent pendingIntent=PendingIntent.getActivity(Verify.this,0,intent,0);
    notification.setContentIntent(pendingIntent);

    NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(uniqueID,notification.build());
    //Log.i(TAG, "coming here in notification");
}

I am using MediaSessionCompat instead of MediaSession class. Please check if there is a mistake.

like image 488
Sachin Parashar Avatar asked Dec 20 '15 04:12

Sachin Parashar


People also ask

How do you add buttons to push notifications?

To use action buttons in your Android push messages, create a push campaign and enable notification buttons in the Compose tab. Then click Add Button and specify your button text and On-Click Behavior. You can select from the following available actions: Open App.

Do push notifications have buttons?

What Are Push Notification Action Buttons? This is a case where the name is actually pretty self-explanatory: Push notification action buttons are literal buttons that can be added to the push notifications you send in order to allow recipients to take action on the messages they receive.


1 Answers

You want something like this, right?

enter image description here

For notifications, just use the .addAction(R.drawable.MYDRAWABLE, "BUTTON", INTENT) method.

Notification notification = new Notification.Builder(context)
    // Show controls on lock screen even when user hides sensitive content.
    .setVisibility(Notification.VISIBILITY_PUBLIC)
    .setSmallIcon(R.drawable.NotIcon)
//HERE ARE YOUR BUTTONS
    .addAction(R.drawable.ic_prev, "BUTTON 1", myIntentToButtonOneScreen) // #0
    .addAction(R.drawable.ic_pause, "BUTTON 2", myIntentToButtonTwoScreen)  // #1
    .addAction(R.drawable.ic_next, "BUTTON 3", myIntentToButtonThreeScreen)     // #2
    // Apply the media style template
    .setStyle(new Notification.MediaStyle()
    .setShowActionsInCompactView(1)
    .setMediaSession(mMediaSession.getSessionToken())
    .setContentTitle("Example for you")
    .setContentText("Example for you")
    .setLargeIcon(ButtonExampleIcon)
    .build();

Take a look at this:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Just remember this, in order to customize your notifications, use the .build method. That's what we do in the code I gave you. Let me know if this helped :)

like image 101
Ruchir Baronia Avatar answered Oct 25 '22 16:10

Ruchir Baronia