但是,当收到通知时,通知收件箱样式会更新

时间:2014-08-13 11:37:42

标签: android android-intent notifications google-cloud-messaging

我正在尝试制作一个通知样式,我希望所有通知都在一个引擎盖下,并且它们将以直线显示。如果有新通知,则应该是最新的通知。通知列表应为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());

Target Design

我知道我可以通过在创建

时添加任意数量的行来完成此操作
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());

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

您需要自己做,通过为您的通知分配自己的ID,您以后可以更新它。而且你需要通过连接消息来自己构建通知