Reputation: 87
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;
}
Upvotes: 0
Views: 1402
Reputation: 6792
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.
Upvotes: 1