-4

I want to send data from intent if my intent is null then simple open my main activity. This is my tryout but its not work for me.

From one activity to send

intent = new Intent(this, MainActivity.class);
        intent.putExtra("androidkey",body);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

and receive time

 String uid;
    Intent intent = getIntent();
 uid = intent.getStringExtra("androidkey");

Oncreate method of main activity

 if (uid.equals("hello")){
            startActivity(new Intent(MainActivity.this, About.class));

        }else if (uid.equals("")||uid.equals(null)){

            startActivity(new Intent(MainActivity.this, MainActivity.class));
        }

Please tell me the solution for this because its crash my application.

what actually i want to do in my code is

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        if(remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data: " + remoteMessage.getData());
            sendNotification(remoteMessage.getData().get("message"));


        }

        //Check if the message contains notification

        if(remoteMessage.getNotification() != null) {
            Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }
    }

    private void sendNotification(String body) {
 if (body.equals("Image Upload Successfully")){
            intent= new Intent(this, Image_Gallery.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        }else if (body.equals("Video Upload Successfully")){

            intent = new Intent(this, Video_Gallary.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }else if (body.equals("Home Work Are Uploaded")){

            intent = new Intent(this, HomeWork.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }

when my app is in foreground and notification arrived its open perfect page but when application is not in foreground it's open Mainactivity

so please tell me what should i can do for that..?

  • Please post logcat error. It help everyone to found the issue. – Chirag Jul 03 '17 at 11:14
  • Do not decide which activity to load in MainActivity, when it is already started, but before creating the intent. If you have any android key, start the About_School activity and if not..start the main. So...decide which activity to load at the place where you create the Intent, not in the resulting activity. – JacksOnF1re Jul 03 '17 at 11:15
  • Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference – mayank gajjar Jul 03 '17 at 11:16
  • Possible duplicate of [Notification to activity](https://stackoverflow.com/questions/44793746/notification-to-activity) – JacksOnF1re Jul 03 '17 at 11:59

2 Answers2

0

Sending activity,

    Intent newActivity1 = new Intent(ActivityA.this, ActivityB.class);
newActivity1.putExtra("androidkey", body);
startActivity(newActivity1);

Receiving Activity,

 String value = getIntent().getExtras().getString("androidkey");

Or,

    Bundle extras = getIntent().getExtras(); 
String uid;

if (extras != null) {
    uid= extras.getString("androidkey");

}

For firebase try something like this,

public void onMessageReceived(String from, Bundle data) {
        Bundle bundle = data.getBundle("notification");

                if (bundle != null) {
                   String text = bundle.getString("text");
                    String body = bundle.getString("body");
    }
}

or,

public void onMessageReceived(RemoteMessage remoteMessage)
    {
        Log.e("dataMessage",remoteMessage.getData().toString());
        try
        {
            Map<String, String> params = remoteMessage.getData();
            JSONObject object = new JSONObject(params);
            Log.e("JSON_OBJECT", object.toString());
      }
   }
Sunisha Guptan
  • 1,555
  • 17
  • 44
0

Okay, because a comment only may be to meaningless.

Do not decide where to go, in one of your activities, but at the place where you create the intent.

Like this:

Intent startIntent;
if(body == null || body.isEmpty()){
    startIntent = new Intent(context, MainActivity.class);
}else{
    startIntent = new Intent(context, About.class);
    startIntent.putExtra("androidkey",body);
}

startIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(startIntent);
JacksOnF1re
  • 3,336
  • 24
  • 55