I am using an AlarmManager
to schedule various Notification
s 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;
}
}