Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pending intent extras are received only when activity is paused

I'm sending extra string with pending intent from my StateCh.java to MainActivity. My expectation on it is to display dialog in MainActivity when pending intent with extra is arrived (notification is clicked). The problem is when i open the MainActivity and then I click the notification there is no extras inside pending intent and dialog isn't displayed. When I pause the MainActivity (by pressing back button) and click the notification again it works as expected.

MainActivity.java:

public class MainActivity extends Activity {

 //...

  @Override
protected void onNewIntent(Intent intent)   {
    super.onNewIntent(intent);

    Bundle extras = getIntent().getExtras();
    if(extras !=null) {
        String value1 = extras.getString("message");
        Log.v("alert", value1);

        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("title");
        alertDialog.setMessage(value1);
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                //startActivity(MainActivity.this.getIntent());

            }
        });

        alertDialog.show();
    }
 }
}

StateCh.java:

public class StateCh extends Service {

//...

   private void notificationU(String title, String text)  {

    //The intent to launch when the user clicks the expanded notification
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("message", "something");
    intent.setAction("actionstring" + System.currentTimeMillis());

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

     Notification noti2 = new NotificationCompat.Builder(this)
     .setContentTitle(title)
     .setContentText(text)
     .setSmallIcon(R.drawable.warning)
     .setContentIntent(pendIntent)
     .build();

     mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     mNotificationManager.notify(123456, noti2);
    }

    // ...      

}
like image 584
XorOrNor Avatar asked May 15 '13 10:05

XorOrNor


People also ask

How does Pending Intent work?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

What is intent and pending intent?

PendingIntent is a wrapper of Intent . The foreign app that receives the PendingIntent , doesn't know the content of Intent which is wrapped by PendingIntent . The mission of foreign app is to send back the intent to owner when some conditions are met (For example: alarm with schedule, or notification with click...).

What is a pending intent give example?

A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.


1 Answers

Change Bundle extras = getIntent().getExtras();

To Bundle extras = intent.getExtras();

or call setIntent(intent) first

like image 55
FunkTheMonk Avatar answered Oct 23 '22 14:10

FunkTheMonk