0

I am using GCM creating a notification and when the user taps on notification, I am redirecting him to an Activity(CandyInfoActivity).

This scenario is working flawlessly when the user taps notification from :

1.) anywhere in the app(when the app is opened)

2.) app is in the background(when pressing HOME button)

3.) app is closed(by pressing the BACK< button)

Now the problem is :

When I remove the the app from RECENT APPS Stack(clearing the recent apps by pressing the last remaining button), I am not able to launch the desired activity, instead it starts the DEFAULT activity(MainActivity)

The funny(or my Dumb) part is that when the user clicks on the notification and the MainActivity is launched as in problem scenario, then:

If I press the BACK button, the app closes and the app is opened from the RECENT APPS, selection it shows the Desired activity that needs to be launched when the USER clicks on NOTIFICATION.

I am using the following code for sending Notification:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    final Intent intent = new Intent(this, CandyInfoActivity.class);

    intent.putExtra(Constants.EXTRA_MESSAGE_DATA, cool);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent, 0);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(((BitmapDrawable) this.getResources().getDrawable(R.mipmap.ic_launcher)).getBitmap())
            .setContentTitle(getString(R.string.app_name))
            .setWhen(System.currentTimeMillis())
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setAutoCancel(true)
            .setSound(defaultSoundUri);


    PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
    wl.acquire(15000);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

So, if you understand the scenario, I am displaying the intended activity correctly once the app is in foreground or background, but when I kill(or preferably remove from RECENT APPs Stack)the App and Notification is clicked, the Intended CandyInfoActivity.class is not opening instead MainActivity.class is opening which is the DEFAULT Activity of the App.

P.S: Also I notice my app becomes white for sometime, and in the while, the Logs used/set in the CandyInfoActivity.class are printed on the console and the MainActivity.class is opening. I have been searching here for the past week, so if you have gone through this issue (or) got any suggestions, please post here.

— Sai --

1 Answer 1

0

First of all, make sure that the hierarchy of the activities in XML is set correctly.

Then, build application stack to and pass the pendingIntent from the notification in setContent. This way the user would be able to navigate through the app smoothly after the notification's target activity is utilized. The snippet shows the implementation suggested in the docs where you can find detailed explanation

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());

.

2
  • Hey KayAnn, Thanks for the answer, are you sure that this answer is relevant to my question? " I am not able launch the ResultActivity.class onClick of the notification, when the app is completely removed from the stack. "
    – sai
    Aug 1, 2015 at 10:58
  • Sorry I thought you wanted the app to go back to the previous activity. Can you try putting in a flag like this: PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    – Kay_N
    Aug 4, 2015 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.