Android:创建新通知后,较旧的通知将被替换 [英] Android: After creating a new notification, the older one is replaced

查看:63
本文介绍了Android:创建新通知后,较旧的通知将被替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个通知,而不从我的应用中取消/删除以前的通知.这是我用于创建通知的代码:

I want to create a notification without canceling/deleting previous notifications from my app. Here is my code for creating a notification:

private void notification(Context context, String title, String content) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle(title)
            .setContentText(content);

    Intent resultIntent = new Intent(context, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //Id allows you to update the notification later on.
    mNotificationManager.notify(100, mBuilder.build());
}

推荐答案

您正在使用硬编码的ID进行通知,当然它将取代旧的ID.尝试使用变量ID而非硬编码100.

You are using a hardcoded id for your notification, of course it will replace the old one. Try using a variable ID instead of a hardcoded 100.

mNotificationManager.notify(100+x, mBuilder.build());

或类似的东西.

这篇关于Android:创建新通知后,较旧的通知将被替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆