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));
}
}
}
therefore intent is always null in constructor. After setIntent(null): It's possible to change intent from outside of activity with setIntent() . In all other cases it can't.
putExtra() method is used for sending the data, data in key-value pair key is variable name and value can be Int, String, Float, etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With