バックグラウンドで1分ごとに呼び出されるサービスを使用しています。HTTPリクエストを使用してデータをフェッチし、通知を介してデータについてユーザーに通知します。問題は、前回フェッチして表示したのと同じデータが含まれている場合に通知を作成したくないということです。
私は静的変数のように設定し、最新のリクエストからのデータを保存して、最新のフェッチされたデータと比較し、それが同じかどうか、通知が表示されるかどうかを確認しようとしました。
これは私の通知コードの一部がどのように見えるかです:
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);