2015-06-05 1 views
0

это мой код уведомления:андроид -Как сделать уведомление исчезнет только тогда, когда пользователь нажал на нее

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.ic_launcher; 
    CharSequence tickerText = "Ticker Text"; 
    long time = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, time); 
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL; 
    Context context = getApplicationContext(); 
    CharSequence contentTitle = "Title"; 
    CharSequence contentText = "Text"; 
    Intent notificationIntent = new Intent(this, FistActiivty.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    mNotificationManager.notify(1,notification); 

Как я могу сделать уведомление исчезнет только тогда, когда пользователь нажал на нее, я имею в виду, я не хочу уведомление исчезает, когда пользователь нажимает на кнопку Очистить

благодаря

+1

Вы можете сделать это постоянным уведомлением. И выполните onClick для уведомления, через которое вы очищаете одно и то же уведомление. –

ответ

1

Использование:

// This notification will not be cleared by swiping or by pressing "Clear all" 
notification.flags |= Notification.FLAG_NO_CLEAR; 

, а также важно:

.setOngoing(true) //put this inside your notification builder 

или использование: Notification.FLAG_ONGOING_EVENT; вместо .setOngoing(true)

Это должно помочь вам.

0

общественности статической окончательный ИНТ NOTIFICATION_ID = 1;

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService (Context.NOTIFICATION_SERVICE);

Intent myintent = new Intent (это, название вашей деятельности); например: mainactivity.class

 myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 1, 
       myintent, PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setContentTitle("Notification") 
     .setStyle(new NotificationCompat.BigTextStyle() 
     .bigText(msg)) 
     .setContentText(msg); 


     mBuilder.setAutoCancel(true); // this method is use for disappear notification after tap on it . 

mNotificationManager.notify (NOTIFICATION_ID, mBuilder.build());

Смежные вопросы