Reputation: 3739
I have hooked up to Google Cloud Messaging and am displaying a notification when I receive something. I want to "maximize" my app when the user clicks on the notification. I.e. show the latest activity related to my app. Or, if the app has not started, I want to start it's main activity.
It is imperative that I do not create a new instance of the last activity if the app is already open.
How can I achieve this?
I have seen a lot of similar questions, but all the answers seem to want to specify the activity class, which I don't know, since I don't know which activity was last shown.
Is there a solution to this seemingly simple task?
My code looks something like this at the moment:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("foo")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
But it is not working.
Upvotes: 0
Views: 267
Reputation: 3711
When your opening an Activity
while Notification
is clicked just open the following Activity
. i.e. your PendingIntent
will open following Activity
Please read all the comments
written in Activity
so that you will know why this has been created
public class NotificationHandlerActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//deep linking - resuming app code
if (isTaskRoot()) {
// This Activity is the only Activity, so
// the app wasn't running. So start the app from the
// beginning (redirect to MainActivity)
} else {
// App was already running, so just finish, which will drop the user
// in to the activity that was at the top of the task stack
Intent intent = getIntent();
Uri data = intent.getData();
//you can put your extra's if any
finish();
}
}
}
Upvotes: 1