0

I am using an AlarmManager to schedule various Notifications to the user. It all works well, but I would like to allow the user to tap on the notification and be taken directly to the app. Normally that is easy enough, but in my situation, it's a little more difficult.

Since these notifications will appear at a point in the future (a few days, typically), I do not have any way of knowing whether my app will be active when the notification is posted and when the user taps on it. If the app is active, I want the PendingIntent to take the user to an activity called PostAuthenticationActivity). But if the app is not active, I need the app to go through its usual startup and login routine, which is handled from an activity called SplashScreenActivity. I do not know how to make this PendingIntent smart enough to make this determination.

private Notification getNotification(String title) {

        Intent resultIntent;

        if (Build.VERSION.SDK_INT > 15) {

            // either these lines works on it's own, if I know whether the state of the app
            // how can I build in logic to make this decision dynammically?

            if (appIsRunning)  //pseudocode
                resultIntent = new Intent(this.context, PostAuthenticationActivity.class);
            else
                resultIntent = new Intent(this.context, SplashScreenActivity.class);

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

            Notification.Builder builder = new Notification.Builder(context);
            builder.setContentTitle(title);
            builder.setContentText("Your have a group workout starting in one hour.");
            builder.setSmallIcon(R.drawable.we_run_single);
            builder.setContentIntent(pendingIntent);

            return builder.build();

        } else {
            return null;
        }
    }
AndroidDev
  • 20,466
  • 42
  • 148
  • 239

1 Answers1

0

You can take the user to PostAuthenticationActivity by default, inside of this activity you could check whether authentication is required or not. If auth is required then start your SplashScreenActivity and finish all others.

Intent intent = new Intent(getApplicationContext(), SplashScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent); 

How to start new activity and clear all others

Community
  • 1
  • 1