Android:获取N个推送通知

时间:2014-08-19 08:08:43

标签: java android push-notification

如何显示N个推送通知?我做了以下操作并在循环中调用它。但是当新的到来时,先前的通知会丢失。

代码

int icon = R.drawable.ic_logo;
        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,
                com.example.themomotrail.ShopDetails.class);
        // set intent so it does not start a new activity
        notificationIntent.putExtra("selected", shopName);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        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;
        notificationManager.notify(0, notification);

1 个答案:

答案 0 :(得分:1)

您使用的是相同的Notification id 0

使用不同的身份notificationManager.notify(i, notification); // i = 0,1,2 ......

或生成随机int。

notificationManager.notify((int) (System.currentTimeMillis() / 100000), notification);