2

I want to open a fragment when the app receive a notification for further processing. In my MainActivity.java, getIntent().getExtras() always return null.

MyFirebaseMessagingService.java

  private void sendNotification(String title, String messageBody, String newsID, String newsType) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Bundle extras = new Bundle();
    extras.putString("newsID", String.valueOf(newsID));
    extras.putString("newsType", String.valueOf(newsType));
    intent.putExtras(extras);

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

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

    notificationManager.notify(0, notificationBuilder.build());
}

MainActivity.java

Bundle i = getIntent().getExtras();
    if (i != null) {

        for (String key : getIntent().getExtras().keySet()) {
            String value = getIntent().getExtras().getString(key);

            Log.e("News ID", value);
            if (key.equals("newsID") ) {
                dbHelper.insertNewsID(String.valueOf(value));

                //show news details
                   getSupportFragmentManager()
                        .beginTransaction()
                        .replace(ng.naijaleague.R.id.frame_container, new NewsDetails())
                        .addToBackStack(null).commit();
            }
        }
        String newsType = getIntent().getExtras().getString("newsType");
        if ("newsType".equals(newsType) ) {
            String value = getIntent().getExtras().getString("newsType");

            if("Local".equals(value)){
                //add news type to shared preference
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("newType", "Local");
                editor.apply();
            }else{
                //add news type to shared preference
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("newType", "Foreign");
                editor.apply();
            }
        }
    }

I have followed through all the solutions I found on SO but none of them have worked so far. What must be wrong with the code?

BlackPearl
  • 2,532
  • 3
  • 33
  • 49

3 Answers3

3

POST THIS 4 lines before making PendingIntent Object :P

Bundle extras = new Bundle();
extras.putString("newsID", String.valueOf(newsID));
extras.putString("newsType", String.valueOf(newsType));
intent.putExtras(extras);

LIKE:

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

//PUT HERE
Bundle extras = new Bundle();
extras.putString("newsID", String.valueOf(newsID));
extras.putString("newsType", String.valueOf(newsType));
intent.putExtras(extras);

//THEN Create PendingIntent Object
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Hope it will helps you :)

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
2

Thanks to everyone, all the answers moved me closer to making it work but I needed to change the PendingIntent to the following to nail it.

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Thanks...<3

BlackPearl
  • 2,532
  • 3
  • 33
  • 49
1

move extras assign before Pending intent is created

add flag:

FLAG_ACTIVITY_NEW_TASK

Note than when using PendingIntent.getActivity(...) the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

ceph3us
  • 7,326
  • 3
  • 36
  • 43