How to open an Activity from a java notification on android?

Asked

Viewed 1,106 times

3

How to open an Activity by clicking on the notification. The detail is that I want it to reopen again even if it is open in order to update the data of it. I’m using:

int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);  

2 answers

7


PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

The above code makes use of the flag UPDATE_CURRENT which updates instead of opening a new Activity, it will update the current Activity data. That is, the flag check if the intent and then will open it just updating the data.

  • 2

    @Sergio, yes. The code is the same as Emerson’s. The only thing I added was a FLAG_UPDATE_CURRENT that will verify if Activity exists and if it exists, will update the data from activity, which is what Emerson wants. Quote from him: O detalhe é que eu quero que ela reabra novamente mesmo se ela estiver aberta, a fim de atualizar os dados da mesma. The flag will do just that.

  • thanks man, that’s right. Thank you very much!

  • @Emerson, you’re welcome! haha. :)

0

PendingIntent notificationIntent = preparePendingIntent(context,stringValue,valueBoolean);

PendingIntent preparePendingIntent(Context context,String valueString,boolean valueBoolean) {
    Intent intent = new Intent(context, MainActivity.class);
    Bundle args = new Bundle();
    //Passando Valores para Activity
    args.putBoolean("Key", valueBoolean);
    args.putString("Key", valueString);

    intent.putExtras(args);

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Notification.FLAG_ONGOING_EVENT);

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

    return pendingIntent;
}
  • Welcome to Sopt, Rafael. Code-only responses and links are OK, but they’re even better when you provide context to them. Explain why your code answers the question. Any questions: []

Browser other questions tagged

You are not signed in. Login or sign up in order to post.