title: FCM: Handling Notification in background to get data message into the intent page (received notification)
body: <p>I am sending a notification using the cloud functions to the users. when the user clicks on the notification it should redirect to the Intent page and displays the data message. I am getting a problem when the app is in background </p>
  
  <blockquote>
    <p>MyFirebaseMesssaging services class</p>
  </blockquote>
  
  <pre><code>import android.app.Notification;
  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 org.json.JSONObject;
  
  import java.util.Map;
  
  public class MyFirebaseMessagingService extends FirebaseMessagingService {
      private static final String TAG = "MY android fcm service";
  
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          String FirstName = "0";
          String Amount = "0";
          if (remoteMessage.getData().size() &gt; 0) {
              Log.d(TAG, "Message data payload: " + remoteMessage.getData());
              FirstName = remoteMessage.getData().get("Name");
              Amount = remoteMessage.getData().get("Amount");
              Log.d(TAG, "From:" + remoteMessage.getFrom());
              Log.d(TAG, "Notification MessageBody:" + remoteMessage.getNotification().getBody());
              createNotification(remoteMessage.getNotification().getBody(),FirstName,Amount);
          }
      }
      private void createNotification(String messageBody, String FirstName, String amount) {
          Intent intent = new Intent( this,ReceivedNotification.class );
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          intent.putExtra("FirstName",FirstName);
          intent.putExtra("Amount",amount);
          PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
                  PendingIntent.FLAG_UPDATE_CURRENT);
          Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
                  .setSmallIcon(R.mipmap.ic_launcher)
                  .setContentTitle("deyaPay")
                  .setContentText(messageBody)
                  .setAutoCancel(true )
                  .setSound(notificationSoundURI)
                  .setContentIntent(resultIntent)
                  .setVisibility(Notification.VISIBILITY_PUBLIC);
          //.setSmallIcon(R.drawable.ic_stat_player)
          // Add media control buttons that invoke intents in your media service
          //.addAction(R.drawable.accept, "Accept",) // #0
          //.addAction(R.drawable.ic_reject, "Reject", ResultActivity.class) ; // ;
  
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
          notificationManager.notify(0, mNotificationBuilder.build());
      }
  }
  </code></pre>
  
  <blockquote>
    <p>ReceivedNotification class(Intent Page)</p>
  </blockquote>
  
  <pre><code>import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.widget.TextView;
  public class ReceivedNotification extends AppCompatActivity {
      private TextView Name;
      private TextView amount;
      public static String TAG = "REceivved";
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          setTitle("Received notifications");
          super.onCreate(savedInstanceState);
          setContentView(R.layout.receivednotification);
          Name = (TextView)findViewById(R.id.textView12);
          amount =(TextView)findViewById(R.id.textView14);
          Intent  i = getIntent();
          if (i != null &amp;&amp; i.getExtras() != null) {
              Bundle extras = i.getExtras();
              String FirstName = extras.getString("FirstName");
              String amount1 = extras.getString("Amount");
              Name.setText(FirstName);
              amount.setText(amount1);
  
          }
  
          }
  
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Cloud functions for sending notification</p>
  </blockquote>
  
  <pre><code>const functions = require('firebase-functions');
  const Firestore = require('@google-cloud/firestore');
  const firestore = new Firestore();
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  const db = admin.firestore();
  exports.splitting  = functions.firestore
      .document('deyaPayUsers/{authid}/Split/{authid1}/SentInvitations/{autoid}')
      .onWrite(event =&gt;{
      var  id = event.params.authid;
      var dbref = db.collection('deyaPayUsers').doc(id);
      var na = dbref.FirstName;
      console.log(na);
      console.log(id);
      var id1 = event.params.authid1;
      var  splitid = event.params.autoid;
      var document = event.data.data();
      var phoneNumber = [];
      for(var k in document){
      phoneNumber.push(document[k].PhoneNumber);
      }
      console.log("The json data is " +phoneNumber);
      var ph1 = document.Invite1.PhoneNumber;
      var amount = document.Invite1.Amount;
      var a = amount.toString();
      console.log(a);
      console.log(document);
      console.log(ph1);
    var deyaPay = db.collection("deyaPayUsers");
    let phQuery = deyaPay.where('PhoneNumber','==',ph1)
  .get()
     .then(snapshot =&gt; {
          snapshot.forEach(doc=&gt;{
              console.log(doc.id, " =&gt; ", doc.data());
                         var userData = doc.data();
                         var userId = doc.id;
                         var FirstName = userData.FirstName;
                           console.log(FirstName);
                           var LastName = userData.LastName;
                            console.log(FirstName);
                         var FullName = FirstName + LastName;
                          console.log(FullName);
                         var Token = userData.FCMToken;
                         console.log(userId);
                         console.log("FCM Token for that phoneNumber" + Token);
                         console.log(userData.PhoneNumber);
                         console.log(ph1 + "Exist In DB");
  </code></pre>
  
  <blockquote>
    <p>//This is for sending notification message</p>
  </blockquote>
  
  <pre><code>   let payload = {       
          notification: {
              title: "Message",
              body:  'request to pay ${(FirstName)}',
              sound: "default",
  
  
          },
          'data':{
                  'Name':FirstName,
                  'Amount':a
          }
      };
  console.log(payload);
     return admin.messaging().sendToDevice(Token, payload).then((response)=&gt; { // This method is used for returning the notification to a specific device
      console.log(Token);
        console.info("Successfully sent notification")
      }).catch(function(error) {
          console.warn("Error sending notification " , error)
          });
                  });
  
   }) .catch(err =&gt; {
                      console.log('Error getting documents', err);
                                     });
  
   });
  </code></pre>
  
  <blockquote>
    <p>Manifest file</p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.bodaty.deyaPay.deyaPay"&gt;
      &lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
      &lt;application
          android:name="com.bodaty.deyaPay.deyaPay.GlobalAmount"
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="deyaPay"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name="com.bodaty.deyaPay.deyaPay.MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;service
              android:name="com.bodaty.deyaPay.deyaPay.MyFirebaseMessagingService"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.firebase.MESSAGING_EVENT"/&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
          &lt;service
              android:name="com.bodaty.deyaPay.deyaPay.MyFirebaseInstanceIDService"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.firebase.INSTANCE_ID_EVENT"/&gt;
              &lt;/intent-filter&gt;
          &lt;/service&gt;
          &lt;meta-data
              android:name="com.google.android.gms.wallet.api.enabled"
              android:value="true" /&gt;
          &lt;activity android:name="com.bodaty.deyaPay.deyaPay.Signup" /&gt;
          &lt;activity android:name="com.bodaty.deyaPay.deyaPay.ResetPasswordActivity" /&gt;
          &lt;activity android:name="com.bodaty.deyaPay.deyaPay.EmailVerification" /&gt;
          &lt;activity android:name="com.bodaty.deyaPay.deyaPay.AddMoney"/&gt;
          &lt;activity android:name=".deyaPay"/&gt;
          &lt;activity android:name=".PaymentMethods"/&gt;
          &lt;activity android:name=".Stripe"/&gt;
          &lt;activity android:name=".ACH"/&gt;
          &lt;activity android:name=".ViewProfile"/&gt;
          &lt;activity android:name=".EditProfile"/&gt;
          &lt;activity android:name=".Split"/&gt;
          &lt;activity android:name=".Display"/&gt;
          &lt;activity android:name=".MessageReceived" /&gt;
          &lt;activity android:name=".SentNotificationReceivedNotification"/&gt;
          &lt;activity android:name=".ReceivedNotification"/&gt;
          &lt;activity android:name=".SentNotification"/&gt;
  
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>