【问题标题】:Notification inbox style however gets updated when a notification comes但是,通知收件箱样式会在收到通知时更新
【发布时间】:2014-08-13 11:37:41
【问题描述】:

我正在尝试制作一种通知样式,我希望所有通知都集中在一个引擎盖下,并且它们将逐行显示。如果有新通知,它应该在第二晚。通知列表应该是 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());

【问题讨论】:

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


    【解决方案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;
    

    【讨论】:

    • 不过我还是得到了标题中的最后一个!!我哪里做错了?
    • @Giye...我按照你说的做,它仍然显示通知中的最后一个...没有任何文字衬里
    【解决方案2】:

    您需要自己执行此操作,通过将自己的 ID 分配给您的通知,您将能够稍后对其进行更新。您还需要自己构建通知,通过连接消息

    【讨论】:

    • 我应该保持相同的 id,然后将最后一条消息与新消息连接起来吗?
    • 我如何获得最后的消息?
    • 是的。您保留相同的 ID,但随后发布新通知(具有相同 ID)和新文本。您需要自己保留旧消息
    • 如何使用 IntentService 中的静态变量来完成任务
    • 感谢您的帮助。如果我没有做课堂作业,我会寻求你的帮助 LOL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    相关资源
    最近更新 更多