0

I have a problem, I want to make a notification for my app. First I build a notification and then if I dont want it then I want to delete it again. How can I do this, that I can delete a specific notification

I start this building from a setOnItemLongClickListener.

Mainactivity:

                            if (isNotificationVisible()){
                                NotificationManager mNotificationManager =
                                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                mNotificationManager.cancel(1122334455);
                            }
                            else {

                                int beginday = begincalendar.get(Calendar.DAY_OF_MONTH);
                                int beginyear=begincalendar.get(Calendar.YEAR);
                                int beginmonth=begincalendar.get(Calendar.MONTH);
                                Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
                                myIntent.setAction(String.valueOf(id));
                                pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
                                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                                alarmManager.set(AlarmManager.RTC, begincalendar.getTimeInMillis(), pendingIntent);

                            }

NotificationService class:

    public class NotificationService extends Service {

    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId)
    {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("title")
                        .setOnlyAlertOnce(true)
                        .setContentText("context")
                        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);


        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1122334455, mBuilder.build());
    }


}

AlarmReceiver class:

    public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service1 = new Intent(context, NotificationService.class);
        context.startService(service1);

    }
}

isNotificationVisible:

private boolean isNotificationVisible() {
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent test = PendingIntent.getActivity(context, 1122334455, notificationIntent, PendingIntent.FLAG_NO_CREATE);
    return test != null;

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3241084
  • 87
  • 1
  • 1
  • 11

1 Answers1

1

Use NotificationManager's public void cancel(int id) method to clear a notification.

So suppose if you want to cancel this notification on a button click then add the below code in your onClick() method,

mNotificationManager.cancel(1122334455); // parameter is your notification ID

For more details see here.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • can i also check if a notification just exists and only then delte it. I have a listview and for every row a notification could be done? – user3241084 Feb 25 '14 at 13:45
  • Ther's no direct method for it but definitely theres a work around. Check http://stackoverflow.com/questions/12654820/is-it-possible-to-check-if-a-notification-is-visible-or-canceled – Atul O Holic Feb 25 '14 at 13:47
  • Just edited now could you pleas lokk where my mistake is – user3241084 Feb 25 '14 at 14:17
  • I am taking a look. Meanwhile I checked and found another way called deleteIntent which is called when notification is deleted. http://stackoverflow.com/questions/7465849/how-to-use-notification-deleteintent – Atul O Holic Feb 25 '14 at 14:28