新通知被刷走后不显示

问题描述 投票:0回答:1

我的 Android 应用程序跟踪用户的位置并将其与列表中的位置进行比较,如果找到匹配项,它会创建一个在屏幕顶部保持可见的平视通知(此图中的绿色带)。

然后,如果用户点击通知,它将使用pendingIntent启动另一个活动(DisplayActivity)。此过程可能会在一次旅程中重复多次,并且每次都会有一个新的通知。

这是我的代码:

void makeNotification() {
    if (SDK_INT >= Build.VERSION_CODES.O) {
        int soundName;
        String NOTIFICATION_CHANNEL_ID = "AMJnotifychannel";
        Intent goShow = new Intent(this, DisplayActivity.class);
        goShow.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        goShow.putExtra("placeno", placeNumber);
        goShow.putExtra("plname", placeName);
        goShow.putExtra("justquiz", quizOnly);
        goShow.putExtra("addedscore", scoreAdded);
        goShow.putExtra("killlquiz", false);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationNumber, goShow, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.expandednotify);
        expandedView.setTextViewText(R.id.placename, placeName);
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        soundName = R.raw.alert;
        Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + soundName);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "amjchannel", NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(sound, audioAttributes);
        channel.shouldVibrate();
        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC); 
        notificationManager.createNotificationChannel(channel);
        builder.setSound(sound);
        builder.setSmallIcon(R.mipmap.amjnotify);
        builder.setContentIntent(pendingIntent);
        builder.setCustomContentView(expandedView);
        builder.setCustomBigContentView(expandedView);
        builder.setAutoCancel(true);
        builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);
        builder.setFullScreenIntent(pendingIntent, true);
        builder.setOngoing(true);
        builder.setChannelId(NOTIFICATION_CHANNEL_ID);
        notificationManager.notify(717, builder.build());
    }
}

只要用户每次点击通知并启动 DisplayActivity,就可以正常工作 - 新通知会继续以平视通知的形式显示在屏幕顶部,并且用户无需打开通知抽屉即可看到他们。

但是,如果用户在任何时候通过向上滑动并离开屏幕来消除通知,则下次出现通知时,其行为会有所不同。将通知滑入抽屉一次意味着,此后,所有未来的通知将不再自动出现在屏幕顶部 - 它们直接进入抽屉,然后用户必须打开抽屉才能看到它们。

我已经使用模拟器在许多 Android 版本上进行了尝试 - API28、API29、API30 和 API33 - 总是得到相同的结果。

这些通知对于我的应用程序至关重要,没有它们就毫无用处。谁能告诉我如何确保每次都有新的通知出现在屏幕上,如图所示,即使用户将通知滑入通知抽屉后也是如此。

只是为了让谜题变得更加复杂..............

我刚刚尝试使用以下方法删除旧通知:

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(717);

在创建新的之前。和以前一样,如果用户每次点击通知并打开新活动,一切都正常。但是,如果他将通知滑开一次,那么每当此后有另一条通知时,它就会直接进入 displayActivity,而无需等待用户点击意图。

我完全困惑了!

android notifications
1个回答
-1
投票

您有解决这个问题的方法吗?我也遇到同样的问题了

© www.soinside.com 2019 - 2024. All rights reserved.