我正在尝试像whatsapp那样实施群组通知。但是这里的问题是用户可以看到前6个通知。如何显示最后6个通知而不是第一个通知。
PFB演示代码:
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import static android.R.attr.value;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public class MainActivity extends AppCompatActivity {
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
private final int NOTIFICATION_ID = 237;
private static int NOTIFICATION_ID1 = 237;
final static String GROUP_KEY_EMAILS = "group_key_emails";
static int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
}
public void send(String message)
{
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(message+"Lanes");
builder.setContentText(message+"Notification from Lanes"+value);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
inboxStyle.setBigContentTitle(message+"Enter Content Text");
inboxStyle.addLine(message+"hi events "+value);
builder.setStyle(inboxStyle);
nManager.notify("App Name",NOTIFICATION_ID ,builder.build());
}
}
答案 0 :(得分:1)
使用字符串数组存储新的通知消息 每次生成新通知时,取消通知和 从列表末尾向通知构建器添加消息 我试过这个并且它有效!
private final int NOTIFICATION_ID = 237;
ArrayList<String> notificationText = new ArrayList<>();
Notification.InboxStyle inboxStyle ;
static int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
send(++i+"");
}
public void send(String message)
{
notificationText.add(message);
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nManager.cancelAll();
Notification.Builder builder = new Notification.Builder(this);
inboxStyle = new Notification.InboxStyle();
builder.setContentTitle(message+"Lanes");
builder.setContentText(message+"Notification from Lanes"+value);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
inboxStyle.setBigContentTitle(message+"Enter Content Text");
for(int i=notificationText.size()-1;i>=0;i--){
inboxStyle.addLine(notificationText.get(i)+"hi events "+value);
}
builder.setStyle(inboxStyle);
nManager.notify("App Name",NOTIFICATION_ID ,builder.build());
}