0

In my Activity I set time for AlarmManager to generate a notification on future date and also stores the information in sqlite db. After setting multiple notification by changing ids to notify for notificationManager I show information in ListView.

Now I want cancel the or delete some notifications. For example I set time for notification for after 1 hour and I want to cancel it on longitemclick of listview but notification generates .

I saw the way how to cancel notification from notification bar but that not suitable for my case. I want to cancel notification before it is going to generate.

How can i achieve this?

here is my code for calling notification:

custom = new CustomDateTimePicker(noticall.this,new CustomDateTimePicker.ICustomDateTimeListener() {

               @Override
               public void onCancel() { 
                   finish();
               }


               @Override
               public void onSet(Dialog dialog, Calendar calendarSelected,Date dateSelected, int year, String monthFullName,
                            String monthShortName, int monthNumber, int date,
                            String weekDayFullName, String weekDayShortName,
                            int hour24, int hour12, int min, int sec,
                            String AM_PM) {

                      Calendar calendar =  Calendar.getInstance();
                      calendar.set(year, monthNumber, date, hour24,  min, 0);

                      long when = calendar.getTimeInMillis();         // notification time


                      myIntent = new Intent(noticall.this, MyReceiver.class);
                      pendingIntent = PendingIntent.getBroadcast(noticall.this, 0, myIntent,0);

                      alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                      alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);


        Date = calendarSelected.get(Calendar.DAY_OF_MONTH) + "/" + (monthNumber+1) + "/" +year; 
        Time = hour12 + ":" + min + " " + AM_PM;


        setlisto(Date);
        Intent intent = getIntent();
        finish();
        startActivity(intent);    

        Toast.makeText(noticall.this,"Record Added", Toast.LENGTH_SHORT).show();

           }
    });      

MYRECIEVER CLASS:

public class MyReceiver extends BroadcastReceiver{

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

        Log.i("App", "called receiver method");
        try{
            Utils.generateNotification(context);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

Utils class:

public class Utils {

    //int k = noticall.in2;

    static int i=0;
     static long[] vibrate = {  1000, 1000, 1000, 1000, 1000 };
     public static NotificationManager mManager;

    @SuppressWarnings("static-access")
    public static void generateNotification(Context context){ 




        mManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        int j=i+=1;
        Intent intent1 = new Intent(context,Sipdroid.class);
        Notification notification = new Notification(R.drawable.yel1," NOTIFICATION!", System.currentTimeMillis());
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, "xxxx", "xx "+noticall.Names, pendingNotificationIntent);
        Log.i("increeeee", noticall.incre+"");
        mManager.notify(j, notification); // for multiple notifications... change 0 to no of notification.... 
    }

}

here in my this activity i want cancel the set notification:

this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
                    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                        Log.w("DELETED", " DELETED");

        // code here to delete specific notification or pendingIntent or alarmmanager


   }
                });   

i tried this code but it worked for cancel all the pending intents to cancel i want to cancel specific pendingIntents:

public void unsetAlarm(View v) {
            myIntent = new Intent(noticall.this, MyReceiver.class);
              pendingIntent = PendingIntent.getBroadcast(noticall.this, 0, myIntent,0);

              alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

              alarmManager.cancel(pendingIntent);
           Log.v("ahooooo", "cancelling notification");
       } 

how can cancel specific pendingIntents?

Addi.Star
  • 441
  • 1
  • 11
  • 32

2 Answers2

1

You cant get the actual pending intent from alarm manager but you can cancel it. Recreate the intent with the exact same data and id's that you used earlier and then pass the intent to cancel function of AlarmManager which will allow you to cancel the notification.

AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);

where pendingIntent is the intent you want to cancel.

some links to refer :- How to get and cancel a PendingIntent?

Cancelling a PendingIntent

Community
  • 1
  • 1
  • Can i keep record of pending intents which i am setting and later i can cancel only those i need to cancel ? it possible ? can you show me a example ? – Addi.Star Jul 20 '16 at 20:11
0

okay i got to solve my problem myself. we can manage multiple pendingIntent

 public void setAlarm(long timu,int id2){

              myIntent = new Intent(noticall.this, MyReceiver.class);
              pendingIntent = PendingIntent.getBroadcast(noticall.this, id2, myIntent,0);

              alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
              alarmManager.set(AlarmManager.RTC, timu, pendingIntent);


        }


        public void unsetAlarm(View v,int id) {


              myIntent = new Intent(noticall.this, MyReceiver.class);
              pendingIntent = PendingIntent.getBroadcast(noticall.this, id, myIntent,0);

              alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

              alarmManager.cancel(pendingIntent);

       } 

i have assigned same ids on creating a pendingIntents and for deletion i read the assigned id and cancel the alarmManager in order to turn off the notification before it is going to trigger .

as `pendingIntent = PendingIntent.getBroadcast(this, id, myIntent,0);

id is the requestCode for PendingIntent we can keep track of it and for cancelation of desired PendingIntent just pass the id of one which you want to cancel .

might help someone . Cheers . Happy Android Coding.

Addi.Star
  • 441
  • 1
  • 11
  • 32