0

This is the code of my notification. I want that onClick my application starts. Right now nothing happen

private void CheckNoti(){ 
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                                service.this);
                notificationBuilder.setContentTitle("Title");
                notificationBuilder.setContentText("Context");
                notificationBuilder.setTicker("TickerText");
                notificationBuilder.setWhen(System.currentTimeMillis());
                notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon);

                Intent notificationIntent = new Intent(this, service.class);
                PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                                notificationIntent, 0);

                notificationBuilder.setContentIntent(contentIntent);

                notificationBuilder.setDefaults(Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);

                mNotificationManager.notify(1,
                                notificationBuilder.build());
    }   }

This is a code inside a class. It's not inside the MainActivity. So i can't do something like:

intent.setClassName("your.package.name", "ActivityToLaunch");

i think because i already doing Intent notificationIntent = new Intent(this, service.class);

David_D
  • 1,404
  • 4
  • 31
  • 65
  • 1
    why dont you start the activity and then start the service in the activity? It would make more sense to do it that way – tyczj Jul 17 '13 at 14:52

1 Answers1

0

To bring your app to the foreground if it is running already you need to set different flags on your intent:

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

For running a specific method you could just pass extra information along with the intent and interpret it in your application to decide which method to run.

See the answer here

Community
  • 1
  • 1
marshallino16
  • 2,645
  • 15
  • 29