In section #1 you're adding this value:
notificationIntent.putExtra("activate",message.toString());
while in receivemessage class (by the way, bad naming, class names should be camel case) you're having:
Intent i=getIntent();
saveit = i.getStringExtra("run");
maybe you should have there:
saveit = i.getStringExtra("activate");
The idea is that from what you have posted, it is not clear if any component is actually providing this run
intent string extra.
EDIT Using your code so that the above notification manager is triggered from an activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_some_action).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setupClick();
}
});
}
private void setupClick() {
String message = "Sample notification";
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = getString(R.string.app_name);
Intent notificationIntent = new Intent(this, MySoActivity.class);
// set intent so it does not start a new activity
notificationIntent.putExtra("run", message.toString());
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent intent = PendingIntent.getActivity(this.getApplicationContext(), 0,
notificationIntent, 0);
notification.setLatestEventInfo(this.getApplicationContext(), title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
}
and the result in a MySoActivity
class:
public class MySoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_so_layout);
TextView lblIntentExtra = (TextView) findViewById(R.id.lblIntentExtra);
Intent intent = getIntent();
String value = intent.getStringExtra("run");
if (TextUtils.isEmpty(value)) {
value = "NONE@!";
}
lblIntentExtra.setText(value);
}
}
The notification is put alright and when tapping on the notification I am getting the expected value. The only difference in the code is that I am using getApplicationContext()
instead of the context you're having above, but I am not sure how relevant that is. Maybe you can compare the differences and see where you're doing wrong ...