【问题标题】:Android notifications won't stackAndroid 通知不会堆叠
【发布时间】:2016-11-23 00:46:33
【问题描述】:

我正在开发一个从服务器接收消息的应用程序。收到消息时,会出现通知。当收到第二条消息时,它应该堆叠而不是创建一个全新的通知。

我创建了一个接口,该接口具有一个在收到消息时将运行的方法。

Server对象是接收消息的地方,构造函数接受我上面提到的接口。

当我初始化服务器对象时,我传递了一个侦听器接口的新实例,其中被覆盖的方法会创建通知。思考过程是,每次创建新通知时,我都会将NEW_POST_NOTI 整数加一并添加到组中。

我的代码如下所示:

final int PUSHES_GROUP = 67;
int NEW_POST_NOTI = 56;

...

Server server = new Server((msg) -> {
    nm = NotificationManagerCompat.from(ctx);
    Notification noti = new NotificationCompat.Builder(ctx)
        .setSmallIcon(R.drawable.ic_noti)
        .setContentTitle("New Message")
        .setContentText(msg)
        .setGroup(PUSHES_GROUP) 
        .build();
    nm.notify(NEW_PUSH_NOTI++, noti);
});

每次收到消息时都会运行相同的代码,但会为每条消息创建单独的通知,而不是对它们进行分组。我还尝试使用setStyle 使其成为InboxStyle,但我不确定如何动态添加通知。是我的逻辑有问题还是我只是错误地使用了通知 API?

【问题讨论】:

标签: android notifications


【解决方案1】:

答案是创建一个InboxStyle 实例变量,并在每次收到新消息时调用addLine。然后,一旦应用调用onResume,重置InboxStyle

例如:

public class ServerService extends Service {
    ...
    NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
    private static NotificationManagerCompat nm;
    private final Context ctx = Server.this;
    Server server;
    private static int pendingPushes = 0;
    private final int NEW_PUSH_NOT = 2;
    ...
    @Override
    public int onStartCommand(Intent i, int f, final int s) {
        nm = NotificationManagerCompat.from(ctx);
        try {
            server = new Server((msg) -> {
                pendingPushes++; 
                style.setBigContentTitle(pendingPushes +" new pushes");                      
                style.addLine(msg);
                Notification noti = new NotificationCompat.Builder(ctx)
                        .setSmallIcon(R.drawable.ic_noti)
                        .setStyle(style)
                        .setGroupSummary("Click here to view")
                        .setNumber(pendingPushes) //Should make the number in bottom right the amount of pending messages but not tested yet
                        .build();
                nm.notify(NEW_PUSH_NOT, noti);
             });
             server.start();
        } catch(IOException e) {
            e.printStackTrace();
        }
        return START_STICKY;
    }

然后我创建了一个方法来重新启动挂起计数并关闭通知。我在MainActivity 内部的onResume() 中运行它

public static void resetPendingPushes() {
    pendingPushes = 0;
    style = new NotificationCompat.InboxStyle();
    if (nm != null) {
        nm.cancel(NEW_PUSH_NOT);
    }
}

主活动

@Override
protected void onResume() {
    super.onResume();
    ServerService.resetPendingPushes();
}

感谢所有回答的人,你帮助了一堆! 对于有类似问题的任何人,如果我的答案中有错别字,我很抱歉,我很快就从我的单元格中输入了它。

【讨论】:

    【解决方案2】:

    我建议您使用NotificationManager 中使用的Notification ID。此 NotificationID 基本上代表每个应用程序的唯一 ID,因此如果您使用相同的通知 ID,那么您将能够合并所有通知。请尝试以下方法并告诉我。

    static final int MY_NOTIFICATION_ID = 1;
    

    像这样声明一个静态通知 ID。并以同样的方式通知! 所以而不是

    nm.notify(NEW_PUSH_NOTI++, noti);
    

    你写

    nm.notify(MY_NOTIFICATION_ID, noti);
    

    【讨论】:

    • 不仅仅是这个。检查我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2017-06-27
    相关资源
    最近更新 更多