2015-06-05 24 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);

意圖myintent = new Intent(this,「your activity name」);例如: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());