1

i have a service which will display a notification. the notification is being displayed correctly, but nothing happens when I click on the nofitification. It should open an activity in my app. here's my code:

public class ServiceCheckExpensesRecurring extends Service{

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            int mNotificationId = 001;
            Intent myIntent = new Intent(this, MenuDashboard.class);
            myIntent.setAction(Intent.ACTION_VIEW);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pIntent = PendingIntent.getActivity(getBaseContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder n  = new NotificationCompat.Builder(this)
                    .setContentTitle("New mail from " + "test@gmail.com")
                    .setContentText("Subject")
                    .setSmallIcon(R.drawable.common_signin_btn_icon_light)
                    .setContentIntent(pIntent)
                    .setAutoCancel(true);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(mNotificationId, n.build());
            return Service.START_NOT_STICKY;
        }
    }

FYI, MenuDashboard is a fragment (if it helps). And i'm using my sony experia mini with ICS

imin
  • 4,504
  • 13
  • 56
  • 103

1 Answers1

3

Intents can launch Activities (or Services), but not Fragments.

If MenuDashboard is a Fragment, then it won't work.

You should use an Activity instead, and embed the Fragment there.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • ouch. never knew about that. android should have thrown an error at least. so there's no way to load a fragment from notification? – imin Dec 23 '14 at 17:25
  • ermm how to embed fragment inside the activity? the reason I need to launch the fragment is because my app uses navigation drawer, and all the activities inside the menu is a fragment – imin Dec 23 '14 at 17:26
  • @imin In that case, you can just redeliver the Intent to your Activity (the one that has the drawer) then implement `onNewIntent()`. – matiash Dec 23 '14 at 17:27
  • cool... first time I heard about onNewIntent(). But I just googled "onNewIntent call fragment" and I don't think I found any result that will help me calling fragments from my activity. Could you point me to the right direction on this? – imin Dec 23 '14 at 17:37
  • @imin You should be able to include the same code where you add your fragments there. – matiash Dec 23 '14 at 17:42
  • Pass in some extra field in myIntent. When your activity starts, you can check for that field in the intent that started it and do something based on that. So if you want it to display MenuDashboard fragment then have the activity check for that field and display the fragment if it's there – imorsi Dec 23 '14 at 19:12