package app.sinan.yilmaz.firebase.cloudchat;

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

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.firebase.udacity.cloudchat.R;

/**
 * Created by hp-pc on 27.3.2017.
 */

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String notificationTitle = null, notificationBody = null;
        String dataTitle = null, dataMessage = null;

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData().get("message"));
            dataTitle = remoteMessage.getData().get("title");
            dataMessage = remoteMessage.getData().get("message");
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            notificationTitle = remoteMessage.getNotification().getTitle();
            notificationBody = remoteMessage.getNotification().getBody();
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        sendNotification(notificationTitle, notificationBody, dataTitle, dataMessage);
    }


       private void sendNotification(String notificationTitle, String notificationBody, String dataTitle, String dataMessage) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("title", dataTitle);
        intent.putExtra("message", dataMessage);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(notificationTitle)
                .setContentText(notificationBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}


@@ -636,18 +636,16 @@
  TAG = %22
-My
 Firebase
@@ -649,15 +649,20 @@
 aseM
-s
+essagin
 gServ
-i
 ce%22;
@@ -812,394 +812,8 @@
 ull;
-%0A        String dataTitle = null, dataMessage = null;%0A%0A        // Check if message contains a data payload.%0A        if (remoteMessage.getData().size() %3E 0) %7B%0A            Log.d(TAG, %22Message data payload: %22 + remoteMessage.getData().get(%22message%22));%0A            dataTitle = remoteMessage.getData().get(%22title%22);%0A            dataMessage = remoteMessage.getData().get(%22message%22);%0A        %7D
 %0A%0A  
@@ -1442,32 +1442,8 @@
 Body
-, dataTitle, dataMessage
 );%0A 
@@ -1539,46 +1539,8 @@
 Body
-, String dataTitle, String dataMessage
 ) %7B%0A
@@ -1605,102 +1605,8 @@
 s);%0A
-        intent.putExtra(%22title%22, dataTitle);%0A        intent.putExtra(%22message%22, dataMessage);%0A
     
@@ -1733,27 +1733,8 @@
 s, 0
- /* Request code */
 , in
@@ -2028,38 +2028,186 @@
 .set
-SmallIcon(R.mipmap.ic_launcher
+AutoCancel(true)   //Automatically delete the notification%0A                .setSmallIcon(R.mipmap.ic_launcher) //Notification icon%0A                .setContentIntent(pendingIntent
 )%0A  
@@ -2310,45 +2310,8 @@
 dy)%0A
-                .setAutoCancel(true)%0A
     
@@ -2352,58 +2352,10 @@
 Uri)
-%0A                .setContentIntent(pendingIntent)
 ;
+%0A
 %0A%0A  
@@ -2401,32 +2401,16 @@
 anager =
-%0A               
  (Notifi
@@ -2513,33 +2513,8 @@
 fy(0
- /* ID of notification */
 , no
@@ -2549,9 +2549,8 @@
     %7D%0A%7D%0A
-%0A

Update MyFirebaseMessagingService.java