Question

I'm trying to use notifications in my app, but I've got a problem using following code:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification)
        .setContentTitle("title")
        .setContentText("message");
        builder.setOngoing(true);
        Intent resultIntent = new Intent(this,StartActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(StartActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
        builder.setContentIntent(resultPendingIntent);
        notificationManager.notify(1, builder.build());

When I return to homescreen and click the notification in the status bar StartActivity gets destroyed and created again. But, I just want the Activity to show again like clicking the launcher icon would do in this ,moment. How can I change that?

regards

Was it helpful?

Solution 2

Following worked for me:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification)
        .setContentTitle("title")
        .setContentText("message");
        builder.setOngoing(true);
        Intent resultIntent = new Intent(this,NotificationActivity.class);///!!!!
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
        builder.setContentIntent(resultPendingIntent);
        notificationManager.notify(1, builder.build());

NotificationActivity is special class, which finishs immediately in onCreate() and brings the task of the app to foreground. Source: Notification to restore a task rather than a specific activity?

OTHER TIPS

Try adding

resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Before you create the PendingIntent

Source:

How to make notification intent resume rather than making a new intent?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top