2016-05-06 6 views
1

私はAndroid開発には新しく、WebSocketを使用してサーバーからメッセージを受信し、メッセージを送信するときに通知を受け取るアプリを作成しています。私はそれとすべての仕事をしましたが、私が望む唯一のものは、expandex(GmailやWhatsAppのような)という単一の通知を持つことです。私は過去2日間は検索しましたが、私が探していたものは見つかりませんでした(Android Developersのウェブサイトからも)。誰かがこれをやって私を助けることができる?事前におかげで、私の悪い英語のために申し訳ありませんが、問題の解明が必要な場合はちょうど質問:)Androidに通知を積み重ねる方法は?

電話ピクチャのような何か:http://developer.android.com/intl/es/training/wearables/notifications/stacks.htmlこれを試してみましたが、働いていない..私はそれをコードを投稿することができますあなたが見たい場合は)ここで

は私のコードは、(更新)です:

  int n=0; 
     final static String GROUP = "group"; 
public void notifica(String title,String text) { 

    NotificationCompat.Builder mBuilder = 
      (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.icona) 
        .setContentTitle(title) 
        .setContentText(text) 
        .setStyle(new NotificationCompat.InboxStyle() 
        .addLine(text) 
        .setBigContentTitle(n+" new messages") 
        .setSummaryText("irrigator")) 
        .setGroup(GROUP) 
        .setGroupSummary(true) 
        .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission 
        .setStyle(new NotificationCompat.BigTextStyle() 
          .bigText(text)); 

    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(n, mBuilder.build()); 
} 
n++; 

アップデート2:

Notification notif = new NotificationCompat.Builder(this) 
      .setContentTitle("New mail from a ") 
      .setContentText("a") 
      .setSmallIcon(R.drawable.icona) 
      .setGroup(GROUP_KEY_EMAILS) 
      .build(); 
    NotificationManagerCompat notificationManager = 
      NotificationManagerCompat.from(this); 
    notificationManager.notify(n, notif); 
    n++; 

    Notification notif2 = new NotificationCompat.Builder(this) 
      .setContentTitle("New mail from dsd") 
      .setContentText("td") 
      .setSmallIcon(R.drawable.icona) 
      .setGroup(GROUP_KEY_EMAILS) 
      .build(); 
    notificationManager.notify(n, notif2); 
    n++; 
+0

「機能しなかった」とはどういう意味ですか?あなたは何の結果を見ていますか?第二に、最初のステップは、グループを設定することです、あなたはグループを設定していません。 – cyroxis

+0

コードを更新しました。以前のような新しいメッセージを送信するたびにテキストで新しい通知が表示されます。 – Juan

+0

すべての通知に同じIDを割り当てています。これは、グループに新しいものを追加するのではなく、前のものを単に置き換えるだけです。 – dharms

答えて

0

いくつかの掘削後、私はトンを実現現在、ほとんどのデバイス(一部のウェアラブルを除く)はスタッキングをサポートしていないため、何か手作業で行う必要があります。私がやったことは、SharedPrefsに通知の数を格納し、新しい通知が新しいタイトルで以前のメッセージを上書きするときにメッセージを更新することです。 FCMService.java

SharedPreferences sharedPrefs = Utils.getSharedPrefs(getApplicationContext()); 
int notificationCount = sharedPrefs.getInt("CURRENT_OUTSTANDING_NOTIFICATION_COUNT", 0); 

notificationCount++; 

SharedPreferences.Editor editor = sharedPrefs.edit(); 
editor.putInt("CURRENT_OUTSTANDING_NOTIFICATION_COUNT", notificationCount); 
editor.commit(); 

if(notificationCount>1){ 
    messageTitle = notificationCount+" New Messages"; 
    messageText = "Last Message Content:"+ messageText; 
} 

//send notification with the new title and text 
... 

は、その後、あなたの通知をちょうどカウンタをリセット読み込む活動で、出来上がりすれば完了です。 NotificationViewActivity.java

//somewhere where notifications are read 
SharedPreferences.Editor editor = sharedPrefs.edit(); 
editor.putInt("CURRENT_OUTSTANDING_NOTIFICATION_COUNT", 0); 
editor.commit(); 
関連する問題