views:

70

answers:

1

I am trying to start an activity from the notification bar but it needs to get an int. From activity to activity I would just put an int into the intent but I can't seem to figure out where to add the extra for the notification bar? Is this possible? If not is there a better way to do it?

public void notifyMe() {
    int icon = android.R.drawable.presence_away;
    CharSequence tickerText = "New Task Availible";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "PRISM Task";
    CharSequence contentText = "New Task Availible";
    Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notificationIntent.putExtra("taskId", 20);  //unable to retrieve 20 later

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int HELLO_ID = 1;

    mNotificationManager.notify(HELLO_ID, notification);
}

How I am retrieving:

Intent myIntent = getIntent();
System.out.println("#######TaskID from Intent ===== " + myIntent.getIntExtra("taskId", -1));
+1  A: 

You have to call notificationIntent.putExtras(...) before creating PendingIntent that takes notificationIntent as a parameter. Try this code out:

        Intent notificationIntent = new Intent(this, TextAnswerActivity.class);
        notificationIntent.putExtra("taskId", 20);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new Notification(icon, tickerText, when);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

And to receive the data:

    Bundle b = getIntent().getExtras();
    if (b != null) {
        int taskId = b.getInt("taskId");
        System.out.println("############# taskID: " + taskId);
    }
Maciej Pigulski
Worked perfectly. Thanks.
Mike