我使用的服務在後臺每分鐘都會被調用。它使用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);
你能分享代碼嗎? –