32

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?

Ziem
  • 6,579
  • 8
  • 53
  • 86
yoshi24
  • 3,147
  • 8
  • 45
  • 62

3 Answers3

32

As for yoshi24's comment, you may be able to set extras like this.

final Intent intent = new Intent(this, MyActivity.class);
intent.setData(data);
intent.putExtra("key", "value");
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

You need to be aware of this as well before going for pending intents

https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity

UPDATE some thing like this will work for you

int your mainfest

<activity android:name=".MyActivity" android:launchMode="singleTop" ... />

in your activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    processIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {     
    processIntent(intent);
};

private void processIntent(Intent intent){
    //get your extras
}
Sean Wei
  • 7,433
  • 1
  • 19
  • 39
Samuel
  • 9,883
  • 5
  • 45
  • 57
  • What is the Link you posted refering to, that i am trying to do? – yoshi24 Aug 25 '11 at 04:43
  • 1
    you have to change the intent flag as **android:launchMode="singleTask"** in the manifest and override **onNewIntent(Intent intent)** inside the activity. this will enable you to receive the parameters. – Samuel Aug 25 '11 at 04:49
  • Could you put that in your answer too? – yoshi24 Aug 25 '11 at 04:50
  • 1
    @yoshi24: could you select my response also as answer. i guess we can select more than one. but i dont know if it is possible. **and of-course if it works for you**. – Samuel Aug 25 '11 at 04:58
  • I wish i could, but that would be wrong to take the answer away from the first answer. I want to but i dont think it would be fair. Im sure you will get plenty of vote ups when people come across this – yoshi24 Aug 25 '11 at 05:05
  • 1
    @yoshi24: i didn't mean to takeaway, i guessed to mark two responses as answer, its a tick. looks like check-boxes. ;). – Samuel Aug 25 '11 at 05:12
  • 1
    @yoshi24: i tested it in one of my questions, and oops it **toggles**. – Samuel Aug 25 '11 at 05:14
  • +1 for [onNewIntent(Intent intent)](http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent))! – T.Coutlakis Mar 21 '15 at 13:10
25

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);

Ziem
  • 6,579
  • 8
  • 53
  • 86
momo
  • 21,233
  • 8
  • 39
  • 38
10

I did it,

  • I add Intent.FLAG_ACTIVITY_CLEAR_TOP to new intent

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
    Intent intent = new Intent(this, NoficationDemoActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Bundle bundle = new Bundle();
    bundle.putString("buzz", "buzz");
    intent.putExtras(bundle);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "This is the title",
            "This is the text", activity);
    notification.number += 1;
    notificationManager.notify(0, notification);
    
  • Oncreate i do as follow:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    if(getIntent().getExtras()!=null){
        Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
    }
    
Tai Tran
  • 1,406
  • 3
  • 15
  • 27