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));
}
}
}
I'm going to lean out the window and guess that your problem is here:
You are passing
intent
togetActivity()
and expecting that you will get back aPendingIntent
that matches yourIntent
and includes your extras. Unfortunately, if there is already aPendingIntent
floating around in the system that matches yourIntent
(without taking into consideration yourIntent
extras) thengetActivity()
will return you thatPendingIntent
instead.To see if this is the problem, try this:
This says that if there is already a
PendingIntent
that matches yourIntent
somewhere in the system that it should replace the extras with the ones in yourintent
parameter.(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: