选中时,推送通知标记为已读

时间:2015-03-11 10:13:56

标签: android push-notification system-tray

我正在使用GCM推送通知,如果用户在系统托盘上查看通知并点击通知进行阅读,我希望在系统托盘上读取此通知标记而不是忽略此问题,我该如何实现?任何帮助将不胜感激。 这是我的通知代码

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());

    }    

1 个答案:

答案 0 :(得分:1)

点击“{1}}至update the current notification以”更新“其内容并”将其标记为已读“时,您可以使用notifyID

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

此ID是“唯一ID”,因此当您发送多个具有相同ID的通知时,每个通知都将覆盖前一个通知。