Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close the notification dialog after click the dismiss button ,without calling any activity

IMAGE i have the code. which is working fine and also close the notification. but when i am click on the "dismiss" button, then it open the application and close the dialog. which is not required to me. Actually i want, the notification dialog close after click on the "DISMISS" button without opening any activity.

MainActivity.java

public class MainActivity extends Activity{
    static int numMessages = 0;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notificationmain);
        context = getApplicationContext();

        Button bcustomnotifyaction = (Button)  findViewById(R.id.customnotificationaction);

        bcustomnotifyaction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomNotificationAction();

            }
        });

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void CustomNotificationAction() {
        // Set Notification Title
        String strtitle = "You Got New Notification.";
        // Set Notification Text
        String strtext ="Hi,How are You?";

        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(this, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);
        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        int notificationId = new Random().nextInt(); // just use a counter in some util class...
        PendingIntent dismissIntent = NotificationActivity.getDismissIntent(notificationId, getApplicationContext());

        //Create Notification using NotificationCompat.Builder
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                // Set Icon
                .setSmallIcon(R.drawable.logosmall)
                // Set Ticker Message
                .setTicker(strtitle)
                // Set Title
                .setContentTitle(strtitle)
                // Set Text
                .setContentText(strtext)
                // Add an Action Button below Notification
                .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Dismiss", dismissIntent)
                .addAction(android.R.drawable.ic_menu_info_details, "Info", pIntent)
                // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                // showing action button on notification
                .setPriority(Notification.PRIORITY_MAX)
                .setWhen(0)

                /* Increase notification number every time a new notification arrives */
                .setNumber(++numMessages)

                // Dismiss Notification
                .setAutoCancel(true);

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());

    } 
}

notificationmain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotificationView" >
<Button
    android:id="@+id/customnotificationaction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/customnotificationaction" />
</RelativeLayout>

NotificationView.java

public class NotificationView extends Activity {
String title;
String text;
TextView txttitle;
TextView txttext;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notificationview);

    // Create Notification Manager
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Dismiss Notification
    notificationmanager.cancel(0);

    // Retrive the data from MainActivity.java
    Intent i = getIntent();

    title = i.getStringExtra("title");
    text = i.getStringExtra("text");

    // Locate the TextView
    txttitle = (TextView) findViewById(R.id.title);
    txttext = (TextView) findViewById(R.id.text);

    // Set the data into TextView
    txttitle.setText(title);
    txttext.setText(text);
}
}

notificationview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/lbltitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lbltitle" />

<TextView
    android:id="@+id/lbltext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lbltitle"
    android:text="@string/lbltext" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/lbltitle" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:layout_below="@+id/title"
    android:layout_toRightOf="@+id/lbltext" />

</RelativeLayout>

NotificationActivity.java

public class NotificationActivity extends Activity {

public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(0);
    finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately
}

public static PendingIntent getDismissIntent(int notificationId, Context context) {
    Intent intent = new Intent(context, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(NOTIFICATION_ID, notificationId);
    PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    return dismissIntent;
} 
}

Solution:

I got my solution by using broadcast Receiver .

CODE i changed : I added this code in MainActivity.java CustomNotificationAction() .

//Create an Intent for the BroadcastReceiver
        Intent buttonIntent = new Intent(context, ButtonReceiver.class);
        //Create the PendingIntent
        PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);

And Changed this line From

 .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Dismiss", dismissIntent)

To

.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Dismiss", btPendingIntent)

And Added one Java File

ButtonReceiver.java

public class ButtonReceiver extends BroadcastReceiver  {

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Notification Dialog Closed",
            Toast.LENGTH_LONG).show();
    Log.d("Notification:","Notification Dialog Closed");
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(0);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,  0, new Intent(), 0);
    NotificationCompat.Builder mb = new NotificationCompat.Builder(context);
    mb.setContentIntent(resultPendingIntent);
}
}

And Register Broadcast in Mainfest file

 <receiver android:name=".ButtonReceiver"/>

And my problem Solved :)

You can also download the source code from this link :

Download Source Code

like image 887
Priyanka Avatar asked Jun 28 '16 10:06

Priyanka


People also ask

How do you dismiss a notification?

To dismiss a notification, touch it and swipe left or right. Tap the dismiss icon to dismiss all notifications. On newer versions of Android, you can manage some notifications from the lock screen. Double-tap a notification to open the app or swipe left or right to dismiss the notification.

How to start activity from notification Android?

Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .


2 Answers

should set broadcast receiver to detect your action click without starting activity.

Dismiss Ongoing Android Notification Via Action Button Without Opening App

like image 120
Shahbaz Hashmi Avatar answered Oct 28 '22 12:10

Shahbaz Hashmi


You can do it with both service and Broadcast receiver , But I think Broadcast receiver would be a better choice to do it.

 int final MY_NOTIFICATION_ID = 1;

        //Create an Intent for the BroadcastReceiver
        Intent buttonIntent = new Intent(context, AutoDismissReceiver.class);
        buttonIntent.putExtra("notificationId",MY_NOTIFICATION_ID);

           //Create the PendingIntent
        PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);

       //Pass this PendingIntent to addAction method of Intent Builder
        NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
        .....
        .....
        mb.addAction(R.drawable.ic_dismiss, "Dismiss Action", btPendingIntent);
        manager.notify(MY_NOTIFICATION_ID, mb.build());

And Broadcast receiver for above function

  public class AutoDismissReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent) {
                int notificationId = intent.getIntExtra("notificationId", 0);
                NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                manager.cancel(notificationId);
            }
        }

Hope this helps you.

like image 35
Mahendra Chhimwal Avatar answered Oct 28 '22 12:10

Mahendra Chhimwal