5

I'm trying to run an activity through a notification and event onCreate I would like to "redirect". To this add a thought on information in the Intent class. An important feature is that the class that generates the notification is performed through a service. I retrieve the context from getApplicationContext method provided by the class android.app.Application. Whenever I call method getExtras() is returning null. What am I doing wrong?

public class OXAppUpdateHandler {

    private void addNotification(Context context, int iconID,
           CharSequence tickerText, CharSequence title, CharSequence content) {

        CharSequence notificationTicket = tickerText;
        CharSequence notificationTitle = title;
        CharSequence notificationContent = content;

        long when = System.currentTimeMillis();

        Intent intent = new Intent(context, MainActivity_.class);
        intent.setFlags(
            Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(OPEN_UPDATE_ACTIVITY_KEY, 1);

        PendingIntent pendingIntent = 
            PendingIntent.getActivity(context, 0, intent, 0);

        NotificationManager notificationManager = 
            (NotificationManager) context.getSystemService(
                Context.NOTIFICATION_SERVICE);
        Notification notification = 
            new Notification(iconID, notificationTicket, when);
        notification.setLatestEventInfo(context, notificationTitle, 
                                        notificationContent, pendingIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    public static boolean isUpdateStart(Intent intent) {
        Bundle bundle = intent.getExtras();
        boolean result = bundle != null && 
                         bundle.containsKey(OPEN_UPDATE_ACTIVITY_KEY);
        if (result) {
            bundle.remove(OPEN_UPDATE_ACTIVITY_KEY);
        }
        return result;
        }
    }

    @EActivity(R.layout.activity_main)
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (OXAppUpdateHandler.isUpdateStart(getIntent())) {
                startActivity(new Intent(this, UpdateActivity_.class));
            }
        }
    }
JJD
  • 50,076
  • 60
  • 203
  • 339
adrianosepe
  • 55
  • 1
  • 6

2 Answers2

22

I'm going to lean out the window and guess that your problem is here:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

You are passing intent to getActivity() and expecting that you will get back a PendingIntent that matches your Intent and includes your extras. Unfortunately, if there is already a PendingIntent floating around in the system that matches your Intent (without taking into consideration your Intent extras) then getActivity() will return you that PendingIntent instead.

To see if this is the problem, try this:

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

This says that if there is already a PendingIntent that matches your Intent somewhere in the system that it should replace the extras with the ones in your intent parameter.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • This worked for me! I also include the code below because the activity was not always recreated: `@EActivity(R.layout.activity_main) public class MainActivity extends OXBaseActivity { ... @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (OXAppUpdateHandler.isUpdateStart(intent)) { startActivity(new Intent(this, UpdateActivity_.class)); } } }` – adrianosepe Jan 16 '13 at 10:38
  • 1
    The activity isn't always recreated because you've set both `Intent.FLAG_ACTIVITY_CLEAR_TOP` and `Intent.FLAG_ACTIVITY_SINGLE_TOP`. If you want the activity to always be recreated, remove `Intent.FLAG_ACTIVITY_SINGLE_TOP` from the `Intent` you pass to `getActivity()`. – David Wasser Jan 16 '13 at 11:26
  • It worked. I was passing 0 instead of PendingIntent.FLAG_UPDATE_CURRENT. Many thanks. – Edison Santos Jan 30 '14 at 14:00
  • 1
    You saved me a lot of time, @DavidWasser Thank you! – GVillani82 Jul 31 '14 at 15:29
  • 1
    @David Wasser You save me. Thanks a lot. – Smeet Jan 11 '16 at 11:45
-1

(1) Check here to make sure you are using the put/get extras correctly as I don't see the code where you are adding data the intent.

(2) It looks like you are not calling get intent and get extra and, as a result, not actually getting anything from the bundle (assuming that information extists). If you are checking for a boolean value you should get the data you place in the bundle as follows:

Bundle bundle = getIntent().getExtras();

if (bundle.getBooleanExtra("WHATEVER"){
   //whatever you want to do in here
} 
Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
  • I add the information in `... intent.putExtra (OPEN_UPDATE_ACTIVITY_KEY, 1);` and then recover `Bundle bundle = intent.getExtras (); boolean result = bundle! = null && bundle.containsKey (OPEN_UPDATE_ACTIVITY_KEY);` – adrianosepe Jan 15 '13 at 15:34
  • But you're not getting the extra correctly. . . did you try the way I suggested? – Rarw Jan 15 '13 at 17:39
  • In my case when I call getIntent().GetExtras() is returning null – adrianosepe Jan 15 '13 at 17:48