2012-10-19 173 views
39

我剛開始使用通知,現在我試圖刪除通知並在通知中心點擊通知後啓動應用程序。點擊後刪除通知

我試着用下面的代碼工作:

import android.app.NotificationManager; 

public class ExpandNotification { 
    private int NOTIFICATION = 546; 
    private NotificationManager mNM; 

    public void onCreate() { 
     mNM.cancel(NOTIFICATION); 
     setContentView(R.layout.activity_on); 
     //Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show(); 
    } 

我想挖掘時代碼執行其他類?

PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0); 

但是,通知不會消失,應用程序也不會啓動。 但我能刷卡它向左或向右將其刪除,但是這不是我想要的..

回答

71

使用標誌Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when); 
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); 

// Cancel the notification after its selected 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

,並啓動應用程序:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

// Create a new intent which will be fired if you click on the notification 
Intent intent = new Intent(context, App.class); 

// Attach the intent to a pending intent 
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

謝謝,這個工作:) – user1756912

+0

很高興幫助!:) – input

+2

如果您不指定ContentIntent(只需要點擊時刪除通知),情況如何?在這種情況下,auto_cancel標誌似乎沒有幫助... –

90

使用Notification.BuilderNotificationCompat.Builder在Builder實例上調用setAutoCancel(true)可獲得相同的效果。

+0

謝謝..它的工作.. –

6

這樣的回答實在是太多了晚,但特意我寫下面的解決方案,因爲通知的構造變得過時,以便使用製造商使用的通知,如下列:

**.setAutoCancel(true)** is used to remove notification on click 

和整個通知就像是follwoing:

private void makeNotification(String title,String msg){ 

    Intent resultIntent = new Intent(this, MasterActivity.class); 

    PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        this, 
        0, 
        resultIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setContentIntent(resultPendingIntent) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle(title) 
        .setAutoCancel(true) 
        .setContentText(msg); 

    int mNotificationId = 001; 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

} 

調用這個方法的標題和消息,你會得到完美的通知。

2

最好&簡單的方法設置builder.setAutoCancel(true)它是取消您的通知後點擊通知。我希望這段代碼能幫助你。

生成器的通知

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setSmallIcon(android.R.drawable.btn_star); 
builder.setContentTitle("This is title of notification"); 
builder.setContentText("This is a notification Text"); 
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 

對於開放活動上點擊通知

Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT); 
builder.setContentIntent(pendingIntent); 
builder.setAutoCancel(true); 

顯示工具的詳情在通知顯示有圖標,圖像通知

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
manager.notify(114, builder.build()); 

完整代碼,標題,說明,自動取消並點擊打開活動

public void ShowIntentNotification(View v) 
    { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setSmallIcon(android.R.drawable.btn_star); 
     builder.setContentTitle("This is title of notification"); 
     builder.setContentText("This is a notification Text"); 
     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 

     Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     builder.setContentIntent(pendingIntent); 
     builder.setAutoCancel(true); 

     NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     manager.notify(114, builder.build()); 

    } 
0

對於我這是

.setPriority(Notification.PRIORITY_HIGH); 

這是造成該通知後不明確的點擊...確保你使用:

.setPriority(Notification.PRIORITY_DEFAULT); 

而且.setAutoCancel(true)應該工作。