Search code examples
androidpush-notificationsystem-tray

Push Notifications mark as read when checked


I am using GCM Push Notification and if user take a look for notification on system tray and clicked on notification for read, i want this notification mark as read on system tray instead of dismiss this, How can i achieve this? any help will be appreciated. Here is my notification code

private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher_logo;
        long when = System.currentTimeMillis();

        pref = new PrefsHelper(context);

        updateNotification = pref.getNotificationID() + 1;

        pref.setNotificationID(updateNotification);

        String title = context.getString(R.string.app_name);
        int diff = (int)System.currentTimeMillis();
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        notificationIntent.setAction(message);

        NotificationManagerCompat notificationManager =  NotificationManagerCompat
            .from(context);



        PendingIntent intent = PendingIntent.getActivity(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder bilder = new Notification.Builder(context);
        bilder.setSmallIcon(R.drawable.ic_launcher_logo);
        bilder.setSubText("");
        bilder.setTicker(message);
        bilder.setContentIntent(intent);

        bilder.setContentTitle(title);
        bilder.setContentText(message);
        /*bilder.setOngoing(true);*/
        bilder.setAutoCancel(false);




        // set intent so it does not start a new activity


        //notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//      notification. (context, title, message, intent);


        // Play default notification sound
        /*notification.defaults |= Notification.DEFAULT_SOUND;

        // notification.sound = Uri.parse("android.resource://" +
        // context.getPackageName() + "your_sound_file_name.mp3");

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;*/

        notificationManager.notify(diff, bilder.build());

    }    

Solution

  • You can use the notifyID to update the current notification when it is clicked to "update" its content and "mark it as read":

    ((NotificationManager) notificationManager).notify(
        notifyID,
        notifyBuilder.build()
    );
    

    This ID is a "unique ID", so when you send multiple notifications with the same ID, each notification will override the previous one.