Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to create a notification that never closes

Tags:

android

I have created notification using NotificationCompat.builder. But i cant set flags through it to make uncancelable.

Can anyone please help me to solve my problem, i would be grateful to you. :)

Thank you :)

noti_intent = new Intent(this, Main.class);
    noti_pend = PendingIntent.getActivity(this, noti_id, noti_intent, 0);
    noti_manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    noti = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.status_ico)
    .setContentTitle("Next alarm is set to ring on")
    .setTicker("Alarm is set to ring")
    .setContentText(next_date.getText()+" "+next_time.getText())
    .setWhen(System.currentTimeMillis())
    .setContentIntent(noti_pend)
    .setAutoCancel(false);
like image 806
Saqib Vohra Avatar asked Dec 12 '22 14:12

Saqib Vohra


2 Answers

Replace setAutoCancel(true) with setOngoing(true).

like image 180
CommonsWare Avatar answered Dec 21 '22 09:12

CommonsWare


Try including the .setOngoing(true) in your code:

noti_intent = new Intent(this, Main.class);
    noti_pend = PendingIntent.getActivity(this, noti_id, noti_intent, 0);
    noti_manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    noti = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.status_ico)
    .setContentTitle("Next alarm is set to ring on")
    .setTicker("Alarm is set to ring")
    .setContentText(next_date.getText()+" "+next_time.getText())
    .setWhen(System.currentTimeMillis())
    .setContentIntent(noti_pend)
    .setOngoing(true)    
    .setAutoCancel(false);
like image 29
Alejandro Colorado Avatar answered Dec 21 '22 09:12

Alejandro Colorado