0

I created this Android notification successfully, however I want to activate my main activity upon touch.

Here is my code.

Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // only needed for activity activation

PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, notificationIntent, 0);

Notification notification = new Notification(R.drawable.floating2, "Click to start launcher",System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "Start launcher" ,  "Click to start launcher", pendingIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

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

notificationManager.notify(ID_NOTIFICATION,notification);

I have checked my work here but I was still unable to start my activity.

Any suggestions?

Community
  • 1
  • 1
Fish
  • 1,689
  • 1
  • 18
  • 28

1 Answers1

2

Instead of

PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, notificationIntent, 0);

use

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64