/*
 * Copyright (c) 2017, Apptentive, Inc. All Rights Reserved.
 * Please refer to the LICENSE file for the terms and conditions
 * under which redistribution and use of this file is permitted.
 */

package com.apptentive.android.example.push;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.apptentive.android.example.ExampleApplication;
import com.apptentive.android.example.R;
import com.apptentive.android.sdk.Apptentive;
import com.apptentive.android.sdk.ApptentiveLog;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

	@Override
	public void onMessageReceived(RemoteMessage remoteMessage) {
		super.onMessageReceived(remoteMessage);
		Log.e(ExampleApplication.TAG, "onMessageReceived()");
		logPushBundle(remoteMessage);
		Map<String, String> data = remoteMessage.getData();

		PendingIntent pendingIntent = Apptentive.buildPendingIntentFromPushNotification(data);
		String title = Apptentive.getTitleFromApptentivePush(data);
		String body = Apptentive.getBodyFromApptentivePush(data);

		if (pendingIntent != null) {
			ApptentiveLog.e("Title: " + title);
			ApptentiveLog.e("Body: " + body);

			Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
					.setSmallIcon(R.drawable.notification)
					.setContentTitle(title)
					.setContentText(body)
					.setAutoCancel(true)
					.setSound(defaultSoundUri)
					.setContentIntent(pendingIntent);
			NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			notificationManager.notify(0, notificationBuilder.build());
		}
	}

	private static void logPushBundle(RemoteMessage remoteMessage) {
		Map<String, String> data = remoteMessage.getData();
		Log.e(ExampleApplication.TAG, "Push Data:");
		for (String key : data.keySet()) {
			String value = data.get(key);
			Log.e(ExampleApplication.TAG, "  " + key + " : " + value);
		}
		Log.e(ExampleApplication.TAG, data.get("title") + ": " + data.get("body"));
	}
}

@@ -1149,16 +1149,72 @@
 ata();%0A%0A
+%09%09if (Apptentive.isApptentivePushNotification(data)) %7B%0A%09
 %09%09Pendin
@@ -1286,32 +1286,66 @@
 fication(data);%0A
+%09%09%09if (pendingIntent != null) %7B%0A%09%09
 %09%09String title =
@@ -1390,16 +1390,18 @@
 (data);%0A
+%09%09
 %09%09String
@@ -1457,39 +1457,10 @@
 );%0A%0A
+%0A
 %09
-%09if (pendingIntent != null) %7B%0A
 %09%09%09A
@@ -1494,16 +1494,17 @@
 title);%0A
+%09
 %09%09%09Appte
@@ -1536,16 +1536,17 @@
 y);%0A%0A%09%09%09
+%09
 Uri defa
@@ -1624,24 +1624,25 @@
 CATION);%0A%09%09%09
+%09
 Notification
@@ -1715,16 +1715,17 @@
 r(this)%0A
+%09
 %09%09%09%09%09.se
@@ -1761,24 +1761,25 @@
 ation)%0A%09%09%09%09%09
+%09
 .setContentT
@@ -1790,16 +1790,17 @@
 (title)%0A
+%09
 %09%09%09%09%09.se
@@ -1819,24 +1819,25 @@
 (body)%0A%09%09%09%09%09
+%09
 .setAutoCanc
@@ -1845,16 +1845,17 @@
 l(true)%0A
+%09
 %09%09%09%09%09.se
@@ -1878,16 +1878,17 @@
 undUri)%0A
+%09
 %09%09%09%09%09.se
@@ -1921,16 +1921,17 @@
 nt);%0A%09%09%09
+%09
 Notifica
@@ -2037,16 +2037,17 @@
 CE);%0A%09%09%09
+%09
 notifica
@@ -2098,16 +2098,226 @@
 ild());%0A
+%09%09%09%7D else %7B%0A%09%09%09%09// This push is from Apptentive, but not for the active conversation, so we can't safely display it.%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09// This push did not come from Apptentive, so handle it as your own push.%0A
 %09%09%7D%0A%09%7D%0A%0A

Update push example.