如何发送多个推送通知并显示它们的数量

时间:2013-11-29 03:25:18

标签: android notifications push-notification multiplying

我有一个带有一些数据的服务器,在我的应用程序中我想通过推送通知显示这些数据,所以问题是我没有得到如何在状态栏中与我的通知合作通知号码。在我的应用中,我从服务器收到ArrayList通知。对于每个通知,我应该使用“通知构建器”,我将在其中放置诸如图标,名称,desc等的通知字段,至少我应该为它们中的每一个调用“NotificationManager.notify”,但是我如何能够显示我刚刚在我的状态栏中收到了3条消息(一个图标的指示符为3,而不是3个图标),并且不会增加通知声音,但在打开状态栏时仍会显示所有这些消息。

我的代码

 public void sendNotifications(ArrayList<Message> messages){
    int notifyID = 0;
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    for(Message message:messages){

        Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Resources res = getApplicationContext().getResources();
        Notification.Builder builder = new Notification.Builder(getApplicationContext());

        builder.setContentIntent(contentIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(res, messages.media))
                    .setTicker("Got a new message")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentTitle(message.titile)
                    .setContentText(message.text); 

        Notification notification = builder.getNotification();
        mNotificationManager.notify(notifyID, notification);
        notifyID++;
    }
}

UPD

为了更加了解我想要的内容,我添加了一个图像

-pic1当我发送通知时 - 图标显示我有多少

When i send notifications

-pic2当我打开状态栏时,它会显示我所有的通知

shows me my notifies

可以这样做吗?

4 个答案:

答案 0 :(得分:1)

你不能这样做 - 将3个通知合并为一个。

您可以创建一个组合所有通知的通知,也可以仅限这样。

您无需获得3次通知。你也可以得到1分或2分。

我在这里看不到问题。如果有3个通知,您将在状态栏中看到3个图标。

每个图标代表下拉通知栏中的一个条目 - 一个图标代表多个条目并不真正有意义。

答案 1 :(得分:0)

Notification.Builder有方法setNumber来设置通知数。它在更新通知部分的Notification docs中进行了介绍。

答案 2 :(得分:0)

您可以使用大视图通知。 点击此处:Notifications

答案 3 :(得分:0)

通知ID必须是常量。如果通知ID不同,它将显示为不同的通知。所以你的代码必须是这样的,声明ID并计算为字段变量:

public static final int NOTIFICATION_ID = 1;
private int notificationCount = 1;

并改变这样的方法:

public void sendNotifications(ArrayList<Message> messages){
        int notifyID = 0;
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        for(Message message:messages){


            Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            // Adds the back stack
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent to the top of the stack
            stackBuilder.addNextIntent(notificationIntent);
            // Gets a PendingIntent containing the entire back stack
            PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_CANCEL_CURRENT);
            Resources res = getApplicationContext().getResources();
            Notification.Builder builder = new Notification.Builder(getApplicationContext());

            builder.setContentIntent(contentIntent)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(res, messages.media))
                        .setTicker("Got a new message")
                        .setWhen(System.currentTimeMillis())
                        .setAutoCancel(true)
                        .setContentTitle(message.titile)
                        .setContentText(message.text);
                        .setNumber(notificationCount++);

           mNotificationManager.notify(NOTIFICATION_ID, builder.build());
        }
    }

由于

相关问题