2012-12-24 42 views
2

我使用的服務在後臺每分鐘都會被調用。它使用HTTP請求獲取數據,然後通過通知通知用戶有關數據。問題是如果包含與上次已獲取並顯示的數據相同的數據,我不希望創建通知。Android通知避免不必要的通知更新

我試圖設置類似靜態變量,並存儲來自最新請求的數據,以將其與最新獲取的數據進行比較,並以這種方式知道它是否相同以及是否顯示通知。

這是我的一些通知的代碼如下所示:

Intent notificationIntent = new Intent(NotificationService.this, MainMenu.class); 
PendingIntent contentIntent = PendingIntent.getActivity(NotificationService.this, NOTIFICATION_REQUESTS_KEY, notificationIntent, 0); 
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
notification.flags = Notification.FLAG_AUTO_CANCEL; 
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);; 
if (!powerManager.isScreenOn()) 
{ 
     mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE, "NotificationWakeLock"); 
     mWakeLock.acquire(10000); 
     WakeLock mWakeLockCPU = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NotificationCPUWakeLock"); 
     mWakeLockCPU.acquire(10000); 
} 
nm.notify(NOTIFICATION_REQUESTS_KEY, notification); 
+0

你能分享代碼嗎? –

回答

0

保存contentText地方,每次你從服務器獲取數據的時間,通知用戶之前,要確保,如果它是一個不同的contentText 。如果它是一樣的不通知。

+0

我不明白我應該如何存儲contentText原因,因爲我瞭解每次調用時都會在新實例中創建服務。否則,就像我上面已經嘗試過的那樣,將數據存儲到靜態變量中一樣。 – parek