Search code examples
androidnotificationmanager

How to set click listener for notification?


I am using the following code to launch a notification when a Service is started Via AlarmManager:

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "App";
CharSequence message = "Getting Latest Info...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
    "Getting Latest Info...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);

How do I set an intent for this item so that when the user clicks on it, it would launch a certain activity?


Solution

  • You basically need to put the Activity class as part of your intent into your PendingIntent. Currently your Intent is empty. To redirect to new activity, it should be:

    // This line of yours should contain the activity that you want to launch. 
    // You are currently just passing empty new Intent()
    PendingIntent contentIntent = 
        PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);