Saqib Vohra
Saqib Vohra

Reputation: 366

android how to create a notification that never closes

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);

Upvotes: 5

Views: 8060

Answers (2)

Alejandro Colorado
Alejandro Colorado

Reputation: 6094

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);

Upvotes: 5

CommonsWare
CommonsWare

Reputation: 1006594

Replace setAutoCancel(true) with setOngoing(true).

Upvotes: 10

Related Questions