0

I have an application which starts a service running on the background. This service periodically issues a constant notification. I would like to be able to press that notification and resume the last activity of the application.

I am creating my notifications as follows:

public static  void startNotification(Service service,  String message) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(service);
    if(prefs.getBoolean("pref_NotificationDisplayed", true)){
        // Creates an Ongoing Notification
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(service.getApplicationContext()).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Title").setContentText(message);

        Intent toLaunch = new Intent(service.getApplicationContext(),MainActivity.class);
        toLaunch.setAction("android.intent.action.MAIN");
        toLaunch.addCategory("android.intent.category.LAUNCHER");
        PendingIntent intentBack = PendingIntent.getActivity(service.getApplicationContext(),   0,toLaunch, PendingIntent.FLAG_UPDATE_CURRENT);

        //PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(intentBack);
        NotificationManager mNotificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);

        // Send Notification
        Notification primaryNotification = mBuilder.build();
        primaryNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mNotificationManager.notify(10001,primaryNotification);
    }

}

I have tried the solutions from here and here with no luck.

Every time I press the notification a new activity is started up rather than resuming the old activity. Is this because I am issuing the activity from a service ? or am I making an obvious error above ?

Thanks for the help.

Community
  • 1
  • 1
Filipe Teixeira
  • 3,565
  • 1
  • 25
  • 45

1 Answers1

1

are you try

toLaunch.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

inyour intent. check above flag i dont have tried...

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15