从android中的状态栏上的多个推送通知中获取特定推送通知的ID

时间:2014-02-11 06:07:17

标签: android push-notification

在我的应用程序中,我生成了3个推送通知。以下是我的代码..

private void generateNotification(Context context, String message, String type)
{

    if (type.equals("HR_ABSENCES"))
    {
        int icon = R.drawable.logo_push;
        long when = System.currentTimeMillis();
        NotificationManager notificationManagerAbsence = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = null;
        notificationIntent = new Intent(context, Worklist.class);
        notificationIntent.putExtra("PushNotificationTypeAbsence", "ABSENCE");
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_SOUND;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManagerAbsence.notify(0, notification);
    }
    else if (type.equals("TICKET_APPROVAL"))
    {
        int icon = R.drawable.logo_push;
        long when = System.currentTimeMillis();
        NotificationManager notificationManagerTicket = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = null;
        notificationIntent = new Intent(context, Worklist.class);
        notificationIntent.putExtra("PushNotificationTypeTicket", "TICKET");
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_SOUND;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManagerTicket.notify(1, notification);
    }
    else if (type.equals("TIMESHEET_APPROVAL"))
    {
        int icon = R.drawable.logo_push;
        long when = System.currentTimeMillis();
        NotificationManager notificationManagerTimesheet = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = null;
        notificationIntent = new Intent(context, Worklist.class);
        notificationIntent.putExtra("PushNotificationTypeTimesheet", "TIMESHEET");
        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_SOUND;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManagerTimesheet.notify(2, notification);
    }
}

enter image description here

我的问题是所有通知都已正确显示。单击通知,然后调用Web服务。但我无法找出点击了哪个通知。我添加了通知的意图。但我总是得到第一个通知的价值。有没有办法知道选择了哪个通知?

1 个答案:

答案 0 :(得分:1)

您可以通过待处理意图中的请求代码来解决它

更改第二个请求代码值,您可以通过它来区分它

     PendingIntent intent = PendingIntent.getActivity(context, -------, notificationIntent, 0);

只需查看以下链接

Android: Click event for Status bar notification