2014-11-06 35 views
4

我已經搜索了相當一段時間了,但是我無法找到答案。我的應用程序顯示通知Notification.PRIORITY_HIGH,導致它顯示爲棒棒糖上的單挑通知。棒棒堂:單擊時取消單挑通知

的問題是,點擊通知本身時(即推出了contentIntent)通知被自動清除,即使Notification.FLAG_AUTO_CANCEL集和通知有Notification.FLAG_NO_CANCEL集。我試過各種標誌組合,包括Notification.FLAG_ONGOING_EVENT,但行爲保持不變。

我希望通知成爲'正常'的通知,而不是取消...任何想法如何解決這個問題?該文檔是不是在這個問題上完全清楚......

代碼重現:

private void showHeadsUpNotification() 
{ 
    final Notification.Builder nb = new Notification.Builder(this); 
    nb.setContentTitle("Foobar"); 
    nb.setContentText("I am the content text"); 
    nb.setDefaults(Notification.DEFAULT_ALL); 
    nb.setOngoing(true); 
    nb.setSmallIcon(android.R.drawable.ic_dialog_info); 
    nb.setContentIntent(PendingIntent.getActivity(this, 0, getIntent(), 0)); 

    // Commenting this line 'fixes' it by not making it heads-up, but that's 
    // not what I want... 
    nb.setPriority(Notification.PRIORITY_HIGH); 

    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, nb.build()); 
} 

編輯:我注意到,當應用程序發佈的通知是在前臺,通知成爲一個正常的,就像我所期望的那樣。輕掃單挑通知(無論當前的前臺應用程序)是否也會生成常規通知。

回答

2

就目前來看,我想出了以下解決方案:

  1. 添加一個額外的contentIntent,表明它是從通知發佈。
  2. 檢查推出的額外內容Activity
  3. 如果存在額外情況,請重新發布通知,但請確保它不成爲平視通知。

代碼:

@Override 
protected void onResume() 
{ 
    super.onResume(); 

    if (getIntent().getBooleanExtra("launched_from_notification", false)) { 
     showNotification(false); 
     getIntent().putExtra("launched_from_notification", false); 
    } 
} 

// If your Activity uses singleTop as launchMode, don't forget this 
@Override 
protected void onNewIntent(Intent intent) 
{ 
    super.onNewIntent(intent); 
    setIntent(intent); 
}  

private void showNotification(boolean showAsHeadsUp) 
{ 
    final Intent intent = getIntent(); 
    intent.putExtra("launched_from_notification", true); 

    final Notification.Builder nb = new Notification.Builder(this); 
    nb.setContentTitle("Foobar"); 
    nb.setContentText("I am the content text"); 
    nb.setOngoing(true); 
    nb.setSmallIcon(android.R.drawable.ic_dialog_info); 
    nb.setContentIntent(PendingIntent.getActivity(
      this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)); 
    nb.setPriority(Notification.PRIORITY_HIGH); 

    // Notifications without sound or vibrate will never be heads-up 
    nb.setDefaults(showAsHeadsUp ? Notification.DEFAULT_ALL : 0); 

    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(0, nb.build()); 
} 
1

我怎麼能想象一個簡單的竅門是說你contentIntent做任何動作只是再次發送相同的通知被聲明爲PRIORITY_DEFAULT什麼的。當然通過使用相同的notyId

我前幾天剛剛有同樣的問題...重點在於谷歌已經打算採取這種行爲,這意味着如果你想聲明你的通知與建議PRIORITY_HIGHMAX一樣重要,它說這是一個緊急需要立即治療的病例。因此,在這種情況下,用戶只能通過向左或向右滑動來解除該通知(通知不會出現在通知抽屜中),或者單擊通知本身開始contentIntent(會導致通知消失,因爲您的採取了緊急行動)。

如果有避免這種行爲的方法,對我來說這將是新的。

希望我能幫助

+0

我其實更喜歡的通知不被單挑所有,但在同一時間,我想保留'PRIORITY_HIGH'。由於聲音和/或振動也需要通知成爲單挑,我的方法就是禁用第二次通知。這樣,它的'PRIORITY_HIGH',但不是單挑。 – caspase 2014-12-03 16:09:53