当前位置: 首页 > 知识库问答 >
问题:

为什么在android系统中,每次触碰任何一个通知都会显示最后一个通知

邴俊达
2023-03-14

我尝试在android应用程序中创建通知。创建一个事务后,我调用“shownotification”方法在设备中显示通知。

public void showNotification(String screen, String message) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("screen", screen);
    intent.putExtra("message",message);
    int id = 1;
    try {
        // get latest id from SQLite DB
        id = DbManager.getInstance().getIntDbKeyValue("notif_id");
        if (id < 1) {
            id = 1;
        }
    } catch (Exception e) {
    }
    intent.putExtra("notif_id", id + "");

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification n = new Notification.Builder(this).setContentTitle(getString(R.string.app_name)).setContentText(message)
            .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent).setAutoCancel(true)
            .setStyle(new Notification.BigTextStyle().bigText(message))
            .build();
    // set next running id into SQLite DB
    DbManager.getInstance().setDbKeyValue("notif_id", (id + 1) + "");
    notificationManager.notify(id, n);
}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    try {
        checkIntent(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void checkIntent(Intent intent) {
        try {
            int id = Integer.parseInt(intent.getStringExtra("notif_id"));
            if (id > 0 ) {
                String s = intent.getStringExtra("screen");
                if ("XXXXXX".equalsIgnoreCase(s)) {
                    Fragment f = new MonitoringFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("message", intent.getStringExtra("message"));
                    f.setArguments(bundle);
                    showFragment(f, false);
                }else{
                    showFragment(new XxxxFragment(), false);
                }
            }
        } catch (Exception e) {
        }
}

PendingIntent pIntent=PendingIntent.GetActivity(this,0,intent,PendingIntent.Flag_Update_Current);

如果有其他方法不保留每个通知及其notificationId和消息?

共有1个答案

李弘光
2023-03-14

我找到了为每个意图添加randon requestCode而不是为每个意图使用0的解决方案。

int requestCode=new Random().nextint();
PendingIntent pIntent=PendingIntent.getActivity(this,requestCode,intent,PendingIntent.flag_update_current);

 类似资料:
  • 问题内容: 我正在将推送通知从FCM发送到Android设备,这是通过将POST消息发送到包含JSON正文的FCM来完成的。 如果我发送相同的JSON正文两次,则Android设备将显示两个通知(或三个或四个,…)。但我只想显示一个。 “ collapse_key”应该可以解决这个问题,对吧?(FCM文档) 但是,它应该插入哪里或如何插入? 当前JSON正文: 我已经尝试了多种方式来包含“ col

  • 在我的应用程序中,我想根据用户的需要显示任意数量的每日通知(这是一种医疗提醒)。用户还设置新通知的时间。但我的应用程序目前只显示一个最新通知。你能帮我解决这个问题吗? 我创建新通知的代码(等由用户预先设置): 和我的AlarmReceiver:

  • 问题内容: 我查看了android文档,并查看了StackOverflow中的所有答案,但是,我似乎无法理解为什么我尝试显示的通知没有显示。每当我单击按钮时,应用程序就会崩溃,而不是显示通知,而有人可以告诉我为什么会发生这种情况吗? 这是堆栈跟踪 问题答案: 根据错误消息,您不能将其用作通知通道的ID-该名称专门为未定位API 26或更高版本的应用程序保留,用于发布未连接任何通道的所有通知。 您可

  • 根据Firebase文档: 我的问题是,当应用程序在后台时,通知系统不显示通知。 我没有使用Firebase控制台,因为我无法将数据添加到通知负载中。所以我使用了RESTful客户机API,但还是同样的问题。 请求:

  • 最好的做法是什么,以通知所有的片断,在一些变化的后备栈? 我尝试使用和每个片段订阅事件,然后当发生更改时,主活动向所有订阅者发送post事件。 只有我取消订阅被破坏的片段。 我不喜欢这个解决方案,因为如果backbackback中有很多片段,它可能会同时占用大量的侦听器。 我的应用程序,有无限深入,从一个片段替换到另一个片段(并添加到backstack),您可以再次替换(并添加到backstack