i'm trying to build news application wich is implemented with push notification using GCM. the senario based on the following:
When user clicks on a notification, a costume intent should be lunched contains the body of the notification.
i have figured out the most of the above senario but i still have one issue:
the costume intent is displaying message of the latest notification
My code is :
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, NotificationActivity.class);
// set intent so it does not start a new activity
Bundle b = new Bundle();
b.putString("message", message);
notificationIntent.putExtras(b);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|
Intent.FLAG_ACTIVITY_SINGLE_TOP);
UNIQUE_INT_PER_CALL ++;
PendingIntent contentIntent = PendingIntent.getActivity(
context,
UNIQUE_INT_PER_CALL,
notificationIntent, // add this
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, contentIntent);
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;
Random generator = new Random();
int r = generator.nextInt();
notificationManager.notify(r, notification);
System.exit(0);
}
here how i read the message in the costume intent
Bundle b = getIntent().getExtras();
String message = null ;
if (b != null) {
message = b.getString("message");
//Log.i(null,"message = "+message);
}
i'm new to android development, and the above code is a result of many example (maybe i have some useless code)