2017-10-20 108 views
0

在這裏,我試圖顯示像whatsapp在Android 7.0或更高版本中的聊天消息,但他們沒有像那樣顯示。我通過從Node.js獲取聊天消息來發送本地推送通知。在android中,我使用套接字客戶端來獲取它們併發送推送通知給其他用戶,如果他們的應用程序在後臺。多個消息在一個組中,但當我嘗試創建一個新組時,其他人發送消息將其添加到同一組中。無法在同一組Android Nougat推送通知中添加相同的用戶消息作爲whatsapp

Below is the screenshot

,它只是更新的推送通知的標題我在這裏做:

public static void bundledNotification(String message, String name, String uId) { 
    Context context = ApplicationClass.context(); 
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); 
    ++numberOfBundled; 
    issuedMessages.add(message); 

    //Build and issue the group summary. Use inbox style so that all messages are displayed 
    NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setGroupSummary(true) 
      .setGroup(uId); 

    NotificationCompat.InboxStyle inboxStyle = 
      new NotificationCompat.InboxStyle(); 
    inboxStyle.setBigContentTitle("Messages:"); 
    for (CharSequence cs : issuedMessages) { 
     inboxStyle.addLine(cs); 
    } 
    summaryBuilder.setStyle(inboxStyle); 

    notificationManager.notify(NOTIFICATION_BUNDLED_BASE_ID, summaryBuilder.build()); 

    //Pending Intent 
    Intent notificationIntent = new Intent(context, ChatMessageActivity.class); 
    notificationIntent.putExtra("uid", Integer.parseInt(uId)); 
    notificationIntent.putExtra(context.getString(R.string.notify_id), Integer.parseInt(uId)); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    // Adds the back stack 
    stackBuilder.addParentStack(ChatMessageActivity.class); 
    // Adds the Intent to the top of the stack 
    stackBuilder.addNextIntent(notificationIntent); 
    PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    //issue the Bundled notification. Since there is a summary notification, this will only display 
    //on systems with Nougat or later 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(contentIntent) 
      .setGroupSummary(true) 
      .setStyle(new NotificationCompat.BigTextStyle() 
        .bigText(name)) 
      .setGroup(uId); 

    //Each notification needs a unique request code, so that each pending intent is unique. It does not matter 
    //in this simple case, but is important if we need to take action on a specific notification, such as 
    //deleting a message 
    int requestCode = NOTIFICATION_BUNDLED_BASE_ID + numberOfBundled; 
    if (messagesMap.get(uId) != null) { 
     messagesMap.get(uId).add(message); 
    } else { 
     ArrayList<String> messageList = new ArrayList<String>(); 
     messageList.add(message); 
     messagesMap.put(uId, messageList); 
     notificationManager.notify(NOTIFICATION_BUNDLED_BASE_ID + numberOfBundled, builder.build()); 
    } 

    AppPreferences.setChatNotificationNumber(context, AppUtils.PUSH_CHATS_NOTIFICATION_NUMBER, requestCode); 
    NotificationCompat.Builder childBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(contentIntent) 
      .setStyle(new NotificationCompat.BigTextStyle() 
        .bigText(message)) 
      .setGroup(uId); 

    notificationManager.notify(requestCode, 
      childBuilder.build()); 

} 

從上面的代碼總是建立在第一次和第二次多了一個通知,它開始加入消息轉換爲舊通知。但是,當我試圖將舊消息添加到同一用戶的線程時,它會在主要通知組下創建一條新消息。提前致謝。

回答

0

使用同個setgroup和通知(NOTIFICATION_BUNDLED_BASE_ID)ID(集團英明)

Context context = ApplicationClass.context(); 
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); 
    ++numberOfBundled; 
    issuedMessages.add(message); 

    //Build and issue the group summary. Use inbox style so that all messages are displayed 
    NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle(name) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setGroupSummary(true) 
      .setGroup(1001); 

    NotificationCompat.InboxStyle inboxStyle = 
      new NotificationCompat.InboxStyle(); 
    inboxStyle.setBigContentTitle("Messages:"); 
    for (CharSequence cs : issuedMessages) { 
     inboxStyle.addLine(cs); 
    } 
    summaryBuilder.setStyle(inboxStyle); 

    notificationManager.notify(1001, summaryBuilder.build()); 
+0

其爲基數的通知工作。現在我要爲子部分添加新的通知。請檢查附加的截圖。 –

相關問題