Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a Dialog box on click of a button present inside the notification in android

As you can see Approve/Reject button inside the notification, I want to open a dialog box to confirm the user input without opening any activity.

enter image description here

Here's my code where MyDialog is an Activity but instead of opening this activity I want to open a dialog box.

public void createNotification(View view) {

    Intent yesIntent = new Intent(this, MyDialog.class);
    yesIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
    yesIntent.putExtra("ACTION", 1);
    PendingIntent yesPIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), yesIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent noIntent = new Intent(this, MyDialog.class);
    noIntent.putExtra("ACTION", 0);
    noIntent.putExtra("NOTI_ID", NOTIFICATION_ID);
    PendingIntent nopIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), noIntent, 0);



    NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
            .setContentTitle("New Project Approval")
            .setContentText("Project Description")
            .setContentIntent(PendingIntent.getActivity(MainActivity.this, 0, yesIntent, PendingIntent.FLAG_CANCEL_CURRENT))
            .setSmallIcon(R.mipmap.bell)
            .setAutoCancel(true)
            .addAction(R.mipmap.approve_ic, "Approve", yesPIntent)
            .addAction(R.mipmap.rejecticon, "Reject", nopIntent) ;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, noti.build());

}
like image 659
viddzy Avatar asked Dec 15 '15 14:12

viddzy


4 Answers

If you want to open the dialog without showing activity. consider the following

1.Create an activity and set its Manifest value as

<activity android:name=".MyDialog"
            android:launchMode="singleInstance" android:excludeFromRecents="true"
            android:taskAffinity="" android:theme="@style/Theme.AppCompat.Dialog">
        </activity>
  1. In this activity's oncreate method. create and show the dialog with following builder

     AlertDialog LDialog = new AlertDialog.Builder(this)
                .setTitle("Title")
                .setMessage("Message")
                .setOnCancelListener(this)
                .setOnDismissListener(this)
                .setPositiveButton("ok", null).create();
        LDialog.show();
    
     @Override
        public void onCancel(DialogInterface dialogInterface) {
            if(!MyDialog.this.isFinishing()){
                finish();
            }
        }
    
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            if(!MyDialog.this.isFinishing()){
                finish();
            }
        }
    

Now generate notification using your createNotification(View view) function.

enter image description here

like image 96
Sahil Avatar answered Oct 17 '22 14:10

Sahil


You have no any other way for open directly Dialog because Dialog need context ither from fragment or from activity.

In this scenario you must open one transparent activity and inside that activity you must can create view like dialog.

Step 1: Add the following style In your res/values/styles.xml file (if you don’t have one, create it.) Here’s a complete file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

(the value @color/transparent is the color value #00000000 which I put in res/values/color.xml file. You can also use @android:color/transparent in later Android versions)

Step 2: Then apply the style to your activity, for example:

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
like image 20
Vishal Patoliya ツ Avatar answered Oct 17 '22 14:10

Vishal Patoliya ツ


You can use BroadcastReceiver for pendingIntent. Create your notification like this

private void showNotification(){
    Intent intent = new Intent(this,TestBroadCast.class);
    intent.setAction("Approve");
    //**Add more extra data here if required**
    PendingIntent storePendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_cast_dark,"Archive",storePendingIntent);
    Intent intent1 = new Intent(this,TestBroadCast.class);
    intent1.setAction("Reject");
    //**Add more extra data here if required**
    PendingIntent storePendingIntent1 = PendingIntent.getActivity(this, 0,
            intent1, PendingIntent.FLAG_CANCEL_CURRENT);


    NotificationCompat.Action viewNowAction = new NotificationCompat.Action(R.drawable.ic_cast_dark,"Reject",storePendingIntent1);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notifyID = 1;
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("New Project Approval")
            .setContentText("New Project Description")
            .setSmallIcon(R.drawable.ic_cast_dark);
    int numMessages = 0;
    mNotifyBuilder.addAction(action);
    mNotifyBuilder.addAction(viewNowAction);
    mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());

}

And then your BroadcastReceiver will look like this

public class TestBroadCast extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    String title;
    if(action.equalsIgnoreCase("Approve")){
        title = "Approve title";
    }
    else{
        title = "Reject title";
    }
    AlertDialog a = new AlertDialog.Builder(context)
            .setTitle(title)
            .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // ok button
                    if(action.equalsIgnoreCase("Approve")){
                        //Approve YES action
                    }
                    else{
                        //Reject YES action;
                    }
                }
            })
            .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // cancel button
                    if(action.equalsIgnoreCase("Approve")){
                        //Approve NO action
                    }
                    else{
                        //Reject NO action;
                    }
                }
            }).create();
     //You have to use below line, otherwise you will get "Unable to add window -- token null is not for an application" 
    a.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

    a.show();
}
}

And in you manifest file add the following

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<receiver android:name=".TestBroadCast">
    </receiver>

It should work for you

like image 2
Zaartha Avatar answered Oct 17 '22 14:10

Zaartha


You can also open the activity from pending intent and use the theme translucent. And open the dialog from that activity

public class OffersDialogActivity extends BaseActivity {
    private AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

    }
    @Override
    protected void onResume() {
        super.onResume();
        setUpDialog();
    }

    private void setUpDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();

        dialogBuilder.setView(dialogView);
        alertDialog = dialogBuilder.create();
        alertDialog.setCancelable(false);
        alertDialog.setCanceledOnTouchOutside(false);
        if(!isFinishing())
        {
            alertDialog.show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(alertDialog != null){
            alertDialog.dismiss();
        }
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(alertDialog != null) {
            alertDialog.dismiss();
        }
    }
}

And use theme:

<style name="TransparentTheme" parent="@style/NoActionBarTheme">
        <item name="android:background">@null</item>
        <item name="background">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@null</item>
        <item name="android:windowNoTitle">true</item>
    </style>
like image 1
Jay Kikani Avatar answered Oct 17 '22 13:10

Jay Kikani