package com.kickstarter.services.gcm;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.google.android.gms.gcm.GcmListenerService;
import com.google.gson.Gson;
import com.kickstarter.KSApplication;
import com.kickstarter.R;
import com.kickstarter.libs.PushNotifications;
import com.kickstarter.models.pushdata.Activity;
import com.kickstarter.models.pushdata.GCM;
import com.kickstarter.services.apiresponses.PushNotificationEnvelope;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Enables various aspects of handling messages such as detecting different downstream message types,
 * determining upstream send status, and automatically displaying simple notifications on the app’s behalf.
 */
public class MessageService extends GcmListenerService {
  @Inject Gson gson;
  @Inject PushNotifications pushNotifications;

  @Override
  public void onCreate() {
    super.onCreate();
    ((KSApplication) getApplicationContext()).component().inject(this);
  }

  /**
   * Called when message is received.
   *
   * @param from SenderID of the sender.
   * @param data Data bundle containing message data as key/value pairs.
   *             For Set of keys use data.keySet().
   */
  @Override
  public void onMessageReceived(@NonNull final String from, @NonNull final Bundle data) {
    final String senderId = getString(R.string.gcm_defaultSenderId);
    if (!from.equals(senderId)) {
      Timber.e("Received a message from " + from + ", expecting " + senderId);
      return;
    }

    final PushNotificationEnvelope envelope = PushNotificationEnvelope.builder()
      .activity(gson.fromJson(data.getString("activity"), Activity.class))
      .gcm(gson.fromJson(data.getString("gcm"), GCM.class))
      .build();

    if (envelope == null) {
      Timber.e("Cannot parse message, malformed or unexpected data: " + data.toString());
      return;
    }

    pushNotifications.add(envelope);
  }
}

@@ -521,226 +521,8 @@
 r;%0A%0A
-/**%0A * Enables various aspects of handling messages such as detecting different downstream message types,%0A * determining upstream send status, and automatically displaying simple notifications on the app%E2%80%99s behalf.%0A */%0A
 publ
@@ -804,16 +804,18 @@
 ed when 
+a 
 message 
@@ -825,16 +825,25 @@
 received
+ from GCM
 .%0A   *%0A 
@@ -959,60 +959,8 @@
 rs.%0A
-   *             For Set of keys use data.keySet().%0A
    *

Trim comment