I am absolute beginner to Android. Now I am learning how to use notification with broadcast receiver. To display notification to user, it is working fine. But the problem is the notification never disappear after I click on it. I showing noti to user by interval.
This is my activity class:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawer;
private NavigationView navigationView;
private ImageButton openSidebarBtn,calendarBtn;
private int CALENDAR_DIALOG_ID;
private int year, month, day;
private Fragment fragment;
private String fTag;
private RemainderReceiver remainderReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
remainderReceiver = new RemainderReceiver();
remainderReceiver.startRemainder(this.getApplicationContext());
.
.
.
}
This is my receiver class showing notification:
public class RemainderReceiver extends BroadcastReceiver {
NotificationCompat.Builder notification;
DatabaseHelper dbHelper;
Context context;
@Override
public void onReceive(Context pContext, Intent intent) {
context = pContext;
dbHelper = new DatabaseHelper(context.getApplicationContext());
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TODO_REMAINDER");
wl.acquire();
ArrayList<Task> items = dbHelper.getToDoTasks();
int taskCount = 0;
if(items.size()>0)
{
for(int i=0;i<items.size();i++)
{
String dateStr = items.get(i).getDate();
if(dateStr==null || dateStr.isEmpty())
{
taskCount++;
}
else{
if(dateStr==CommonHelper.getCurrentDateStrInFormat("yyyy-mm-dd"))
{
taskCount++;
}
}
}
}
showNotification(String.valueOf(taskCount));
wl.release();
}
public void startRemainder(Context context)
{
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,RemainderReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
long intervalMilliSec =12960000;//6 hours
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), intervalMilliSec, pi);
}
public void showNotification(String taskCount)
{
notification = new NotificationCompat.Builder(context);
notification.setAutoCancel(true);
notification.setSmallIcon(R.drawable.todo_app_icon);
notification.setWhen(System.currentTimeMillis());
notification.setTicker("You have "+taskCount+" to-do task(s) for today");
notification.setContentTitle("To-do tasks");
notification.setContentText("Please click here to view your to-do tasks");
Intent i = new Intent(context.getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager)context.getApplicationContext().getSystemService(context.getApplicationContext().NOTIFICATION_SERVICE);
nm.notify(1,notification.build());
}
}
It show noti fine. But it never disappears even I click it. It remains like in screenshot.
What is wrong with my code?