2014-02-11 192 views
1

推送通知的一些問題。當我按下其中一個應用程序時,如何解除我的應用程序的所有通知?另外...如果我啓動應用程序(不是來自通知),我如何解除通知?關閉Android推送通知

下面是我如何從我的GcmIntentService類發送通知(如果它是有用的)。

private void sendNotification(Bundle extras) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    NOTIFICATION_ID = Integer.parseInt(extras.getString("id")); 

    Intent in = new Intent(this, PushActivity.class); 
    in.putExtras(extras); 
    in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    String msg = extras.getString("msj"); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      in, PendingIntent.FLAG_UPDATE_CURRENT); 

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

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 
+0

嘗試'mBuilder.cancelAll()' –

+0

mBuiler不允許我做.cancelAll();任何想法? –

+0

爲什麼?去官方[文檔](http://developer.android.com/guide/topics/ui/notifiers/notifications.html) –

回答

1

我不知道,如果你是把出通知對於同一事件,如果是的話,你可以做所謂的「堆積的通知」,當你得到一個Gmail等不第二封或第三封郵件。這一切都在一個通知中,因此將作爲一個消失。這僅在4.1以上才兼容。

這是Google隨堆疊一起提供的代碼。這有什麼幫助?

mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
// Sets an ID for the notification, so it can be updated 
int notifyID = 1; 
mNotifyBuilder = new NotificationCompat.Builder(this) 
.setContentTitle("New Message") 
.setContentText("You've received new messages.") 
.setSmallIcon(R.drawable.ic_notify_status) 
numMessages = 0; 
// Start of a loop that processes data and then notifies the user 
... 
mNotifyBuilder.setContentText(currentText) 
    .setNumber(++numMessages); 
// Because the ID remains unchanged, the existing notification is 
// updated. 
mNotificationManager.notify(
     notifyID, 
     mNotifyBuilder.build()); 
+0

我不需要顯示所有的通知作爲一個單一的。用.setAutoCancel(true)消除一個通知會很容易。任何想法我怎麼能解僱他們所有人?當我啓動應用程序時,如何消除所有這些問題? –

+0

好吧,我看看這個,不是一個壞主意。問題是它僅在4.1以上兼容=( –

+0

確定有取消所有通知的CancelAll(),可以在點擊通知時調用,然後在啓動程序時添加它。以下是關於管理通知的更多信息的鏈接。[link](http://developer.android.com/reference/android/app/NotificationManager.html#cancelAll()) –

0

嘗試android.app.NotificationManager.cancelAll()