0
votes

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)

1
I don't understand what your problem is. Can you specify what you are expecting to get and what you are getting instead?Eran
when i have many notification on the notification center, if user clicks on any notification the notification body should be shown in the costume intent (NotificationActivity). (this what should happend). but in my case if user clicks on any notification the message of the last on appears in the costume intent (NotificationActivity)Mouhamad Lamaa

1 Answers

0
votes

You should read this page> https://developers.google.com/cloud-messaging/android/start

private void sendNotification(String message) {
    Intent intent = new Intent(this, YOURCLASS.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
    PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_park_notification)
            .setContentTitle("Parkuest Message")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

Add this to your method.

@Override
public void onMessageReceived(String from, Bundle data)
    sendNotification(message)
 }