2013-11-29 15 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