通知收件箱风格时通知不过来获取更新(Notification inbox style however

2019-10-20 17:03发布

我试图做,我想下一个头罩一切都会通知的通知的风格,他们将显示行明智。 如果一个新通知到来时,它应该是在第二最新的。 通知的名单应该是5,这意味着只有最新的5将会显示。

我用这下面的代码为这个,我不知道如何做到这一点,我想我的手StackBuilder太然而,我得到了,这会从更高的API比我现在用一个工作。

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("MY MESSENGER").setContentText("MESSAGES");
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("MESSAGES" + " Details");

    inboxStyle.addLine(messagetype + ":" + msg);


mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
mBuilder.setAutoCancel(true);
notificationManager.notify(Integer.parseInt("0"), mBuilder.build());

我知道我可以通过添加任意数量的行做到这一点,同时创造

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();  

不过,我想,当我过会GCMIntentService被调用,这个名单将被更新。

我尝试使用这个下面的代码以及但是,这种没有任何工作的工作

 NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, GcmActivity.class);
notificationIntent.putExtra("title", messagetype);
notificationIntent.putExtra("message", msg);
PendingIntent intent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);



NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(  
        this).setAutoCancel(true)  
        .setContentTitle("MY MESSENGER")  
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentText("MESSAGES");  

 NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();  

for(int j= 0; j<listMessage.size(); j++)
  inboxStyle.addLine(listMessage.get(j));  
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(intent);
notificationManager.notify(0, mBuilder.build());

Answer 1:

我有同样的问题,并通过坚持对数据库的通知时,一个新的到达加载它们,并将它们删除点击或删除固定它。

DatabaseHelper dbHelper = new DatabaseHelper(this);
    Cursor notifications = dbHelper.getAllNotifications();
    NotificationCompat.Builder mBuilder = extractNotifications(title, msg, contentIntent, notifications);
    dbHelper.insertNotification(title + ": " + msg);

private NotificationCompat.Builder extractNotifications(String title, String msg, PendingIntent contentIntent, Cursor notifications) {
    NotificationCompat.Builder mBuilder;
        mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_icon)
                        .setContentTitle(title)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(NOTIFICAITONS))
                        .setContentText(msg)
                        .setAutoCancel(true)
                        .setLights(Color.WHITE, 1000, 5000)
                        .setDefaults(Notification.DEFAULT_VIBRATE |
                                Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
                        .setContentIntent(contentIntent);

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();

        inboxStyle.setBigContentTitle(NOTIFICAITONS);
        while (notifications.moveToNext())
        {
            inboxStyle.addLine(notifications.getString(notifications.getColumnIndex(DatabaseHelper.NOTIFICATION_MESSAGE)));
        }
        inboxStyle.addLine(title + ": " + msg);
        mBuilder.setStyle(inboxStyle);
    return mBuilder;


Answer 2:

你需要做的是自己,通过向您的通知分配自己的ID,你将能够稍后更新它。 而你需要建立自己通知过,通过连接信息



文章来源: Notification inbox style however gets updated when a notification comes