Fcoder
Fcoder

Reputation: 9216

start an activity when clicking on notifications

I make a notification with this code:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//Create your notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText = " message";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText,when);
Context context = getApplicationContext();
CharSequence contentTitle = last_notifs_array[0][2];
CharSequence contentText = "New Message";
PendingIntent pIntent = PendingIntent.getActivity(notifService.this,0, intent1,0);
notification.setLatestEventInfo(context,contentTitle,contentText,pIntent);
// Send the notification
nm.notify(HELLO_ID,notification);

But I want to start an activity when I click on the notification. How can I do this?

Upvotes: 3

Views: 328

Answers (5)

Amit
Amit

Reputation: 717

Use the Intent setClass methods like mentioned below.

Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(getApplicationContext(), Main.class);

Upvotes: 0

Amitabh Sarkar
Amitabh Sarkar

Reputation: 1311

Try this: in place of YourActivity pur your desired activity what you want to invoke

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         // Create your notification
         int icon = R.drawable.ic_launcher;
         CharSequence tickerText = " message";
         Intent notificationIntent
         long when = System
                 .currentTimeMillis();
         Notification notification = new Notification(
                 icon, tickerText,
                 when);
         notificationIntent = new Intent(context,
                    YourActivity.class);
         Context context = getApplicationContext();
         CharSequence contentTitle = last_notifs_array[0][2];
         CharSequence contentText = "New Message";
         PendingIntent pIntent = PendingIntent
                 .getActivity(
                         notifService.this,
                         0, notificationIntent,
                         0);
         notification
                 .setLatestEventInfo(
                         context,
                         contentTitle,
                         contentText,
                         pIntent);
         // Send the notification
         nm.notify(HELLO_ID,
                 notification);

Upvotes: 3

Manish Dubey
Manish Dubey

Reputation: 4306

Use this code, this will work like a charm.

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = "Your application name with notify string.";
        Intent notificationIntent = new Intent(context, GoogleMapActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.flags |= Notification.DEFAULT_LIGHTS;
                notificationManager.notify(0, notification);

Upvotes: 1

the-ginger-geek
the-ginger-geek

Reputation: 7081

You can use this

    final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification notification = new Notification(R.drawable.notification_popup, message, System.currentTimeMillis());
    final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
            new Intent(context, YourActivity.class), Notification.FLAG_AUTO_CANCEL);

    notification.setLatestEventInfo(context, title, message, contentIntent);
    notification.defaults = Notification.DEFAULT_ALL;
    notificationManager.notify(yourId, notification);

It should solve your issue

Upvotes: 1

PaNaVTEC
PaNaVTEC

Reputation: 2503

Declare a PendingIntent:

Intent intent = ... create intent of your activity here ....
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

And then just pass the paramter to your notification.pendingIntent = contentIntent;

Upvotes: 0

Related Questions