Hi DW.

I'm developing an App that uses the FCM notification, but the problem is that my code doesn't seem to get the actual data like when I extract the message or body it shows just the icon only if the notification was received when the App is open, and it shows the title and body if the notification is received when the App is not running.

Basically what I'm trying is to get the message and put that on a textView so that it is seen on the actual App when you click the notification.

Here is my FirebaseMessagingService code:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
      showNotification(remoteMessage.getData().get("body"));
    }

    private void showNotification(String message) {
        Intent i = new Intent(this,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //if(message != "") {
            //i.putExtra("message",message);
        //}
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle(message)
                .setContentText(message)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentText(message).setLights(Color.GREEN, 300, 300)
                .setVibrate(new long[] { 100, 250 })
                // .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setSmallIcon(R.drawable.blogo)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(0,builder.build());

        //startActivity(i);
    }
}

My MainActivity code:

protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        // remove title
       this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       /* getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);*/

        setContentView(R.layout.activity_main);
        txtStatus = (TextView) findViewById(R.id.txtStatus);
//if(getIntent().hasExtra("message")){
if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("message")) {

    Intent intent = new Intent(MainActivity.this,Main3Activity.class);
    //extras.getString("body")
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    String mmsg = "9";
    if (extras != null) {
        for (String key : extras.keySet()) {
            Object value = extras.get(key);
            //Log.d(Application.APPTAG, "Extras received at onCreate:  Key: " + key + " Value: " + value);
            mmsg = extras.getString("message");
        }
        //String title = extras.getString("title");

        if (mmsg != null && mmsg.length() > 0) {
            //getIntent().removeExtra("message");

//
        }
    }
    txtStatus.setText(mmsg);
    intent.putExtra("nmsg", mmsg);
    startActivity(intent);
}else{
txtStatus.setText("Not set");

    ctoken = FirebaseInstanceId.getInstance().getToken();

    String data = "action=check&token=" + ctoken;
    goData = data;
    new MainActivity.MakeNetworkCall().execute("http://my_server_url" + data, "Post");
//    txtStatus.setText("Please wait...");

}

    }

my server script that sends notifications to FCM and down to the App code:

<?php

    $payload = array(
    'to'=>'this_is_the_App_token',     
    'priority'=>'high',
          "mutable_content"=>true,
          "notification"=>array(
                      "title"=> 'Alert title',
                      "body"=> 'This is a body message.',
                      "sound"=> "default",
                      "message"=> "This is the main message"
          ),
          'data'=>array(
                'action'=>'models',
                'model_id'=>'2701',
              )
        );
    $headers = array(
      'Authorization:key=my_auth_key_here',
      'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $payload ) );
    $result = curl_exec($ch );
    curl_close( $ch );
    var_dump($result);exit;
?>

I've managed to figure out what was the problem. The data array didn't have the message so putting the message inside it solved the problem

 data'=>array(
            'action'=>'models',
            'model_id'=>'2701',
 'message'=>'My message here',
               )`
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.