package im.tny.segvault.disturbances;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

/**
 * Created by gabriel on 4/15/17.
 */

public class FCMService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        switch(remoteMessage.getFrom()) {
            case "/topics/disturbances":
                handleDisturbanceMessage(remoteMessage);
                break;
        }
    }

    private void handleDisturbanceMessage(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        if(!data.containsKey("network") || !data.containsKey("line") || !data.containsKey("disturbance")
                || !data.containsKey("status") || !data.containsKey("downtime")) {
            return;
        }

        MainService.startForNotification(getApplicationContext(),
                data.get("network"), data.get("line"), data.get("disturbance"), data.get("status"),
                data.get("downtime").equals("true"), remoteMessage.getSentTime());
    }


}

@@ -165,19 +165,80 @@
 va.util.
-Map
+Date;%0Aimport java.util.Map;%0Aimport java.util.concurrent.TimeUnit
 ;%0A%0A/**%0A 
@@ -941,24 +941,223 @@
 %0A        %7D%0A%0A
+        if(new Date().getTime() - remoteMessage.getSentTime() %3E TimeUnit.HOURS.toMillis(5)) %7B%0A            // discard messages that have been sent more than 5 hours ago%0A            return;%0A        %7D%0A%0A
         Main

Discard old disturbance notifications. Closes #21