2013-11-29 11 views
1

我有一个服务器与一些数据,并在我的应用程序我想通过推送通知显示此数据,所以问题是我没有得到如何合作通知用我的通知在状态栏中输入号码。在我的应用程序中,我的服务器收到的通知为ArrayList。对于每个通知,我应该使用一个“通知构建器”,其中我会把图标,名称,desc等通知字段,至少我应该为他们每个人调用“NotificationManager.notify”,但我怎么可以显示例如,我在状态栏中收到3条消息(一个图标的指示符为3,NOT 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

这是可能做到这一点?

+0

所以基本上你想要一个通知? –

+0

是的,但是如果我发送1条通知我如何显示3条消息的内容?我需要像通知堆栈或通知列表,它会在状态栏中显示1个图标,但当我打开状态栏时,仍然会显示所有通知。 – whizzzkey

+0

因此,Vang在他的回答中提出了建议,您可以看到gmail示例在这方面做了同样的事情。 http://developer.android.com/guide/topics/ui/notifiers/notifications.html –

回答

1

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

您可以创建一个包含所有通知的单个通知或仅此类通知。

这是没有必要,你会得到3通知。你也可以得到1或2。

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

每个图标代表下拉通知栏中的条目 - 一个图标代表多个条目实际上并不合理。

+0

我想你没有明白我的意思,我想要一个图标像[这](http://startandroid.ru/images/stories/lessons/L0099/L0099_070.JPG)多数民众赞成在这显示我的通知计数我得到了,但是如果我打开状态栏,它应该会显示我所有的通知,如[this](https://lh6.ggpht.com/2bZsMpvP_qLjYfTWt_a30BE29XdIJZ-A8DWySjhwVsHEHbdkTddCPaZU2cNmTgPEXhQ=h900)。无论它是什么感谢您的回答 – whizzzkey

+0

mNotificationManager。 notify(notifyID,builder.build()); '此行本身会显示通知前面的计数,这里的enotifyID是计数变量 –

+0

你所要求的是折旧。 –

0

Notification.Builder有方法setNumber来设置通知数。它的描述在Notification docs更新通知部分。

+0

它会像[那]工作(http://startandroid.ru/images/stories/lessons/L0099/L0099_070.JPG) ?当你打开状态栏时会显示一条通知,对吗?可以在状态栏中显示一个通知图标,但打开它时会显示3个不同的通知? – whizzzkey

+0

它会像这样工作http://developer.android.com/images/ui/notifications/updated_notification.png。我相信你可以通过驳回旧的通知并显示3种不同的模拟这种行为。 – Bracadabra

0

您可以使用大视图通知。 请点击这里:Notifications

+0

但它只能工作> = Android 4.1 – whizzzkey

+0

是的,这是一个限制。 :( –

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

感谢

+0

请解释什么是消息类别....或给了我参考...我不明白....谢谢... –

+0

@ExceptionLover:'消息'类只是一个数据类,其中包含像名称,标题等字段。其实我们正在做的是保存吨他的数据是从pushnotification到这个班。 – Jovin

+0

也可以优先保存通知计数或sqlite。在每次通知到达时优先增加计数,如果用户点击通知,请将该值清除为默认值。 – Jovin