2

I'm having a bit of a problem, and having looked in many places I can't seem to find an answer.

I have a service, this service checks for notifications on a site and alerts the user of a new notification. When the user clicks this notification, it should bring them to an Activity (I'll call this CommentActivity)

However, if I click on the notification inside of the app, nothing happens. If I click on the notification outside of the app, it opens up the last activity shown before the app was closed.

Here is my code for displaying the notification:

        Notice n; //Notification from site

        ....
        Intent nIntent = new Intent(Intent.ACTION_MAIN);
        nIntent.setClass(getApplicationContext(), CommentActivity.class);
        nIntent.putExtra("activity_id", n.getFeedID());
        nIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(FlankNotificationService.this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(n.getTitle())
        .setContentText(n.getText())
        .setContentInfo(arg0.getNotificationAmount() + " notification" + (arg0.getNotificationAmount() == 1 ? "." : "s."))
        .setContentIntent(pIntent);

        Notification mm = mBuilder.getNotification();

        mm.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
        mm.tickerText = n.getText();

        ....

        mNM.notify(1558, mm);

So instead of opening up the CommentActivity, it will either resume the app (if the app is not opened) or do nothing (if the app is opened)

Thank you.

Edit 1:

Added Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag to Intent. Still does not fix the problem.

Edit 2:

I found the cause of my problem (which was a rather silly cause). When the CommentActivity starts, if it does not get passed any data from either the savedInstance or the Intent.getExtra, then it will just close out. The reason why I did not think of this at first was because it didn't even show the transition. After fixing the Intent.getExtra problem it now works. Thank you for all the answers.

edkek
  • 221
  • 1
  • 10
  • That's what its supposed to do. Its just like clicking the App icon in launcher again. Mark your activity with `android:clearTaskOnLaunch="true"` in manifest if you want this activity to appear always. – S.D. Jun 22 '13 at 04:45
  • Add that to the CommentActivity? I tried and it still didn't fix the problem. – edkek Jun 22 '13 at 04:53
  • _+1_ here is the best solution http://stackoverflow.com/questions/31448840/android-how-to-open-last-activity-when-tapping-notification – nAkhmedov Jul 22 '15 at 11:00

1 Answers1

0

You may want to add the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag to your Intent. This will ensure that the CommentActivity is the resumed and active Activity when the intent is handled.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • After adding the flag, it still does not fix the problem. I've updated the issue with the new code with the added flag. – edkek Jun 22 '13 at 04:50