无法添加FLAG_NO_CLEAR等标识实现Android永久通知的问题求助
Hey there! I see you're trying to create a permanent notification that users can't dismiss manually, but you're having trouble adding FLAG_NO_CLEAR. Let's break down the solution step by step.
Why Directly Using FLAG_NO_CLEAR Doesn't Work with Notification.Builder
In modern Android API levels (starting from API 11), the Notification.Builder class abstracts away direct flag manipulation with dedicated methods. Instead of setting FLAG_NO_CLEAR directly, you should use the builder's built-in methods to achieve the same effect—it's more compatible and easier to maintain.
Step-by-Step Fix
Here are two reliable ways to make your notification permanent:
1. Use setOngoing(true) (Recommended)
Calling setOngoing(true) on your Notification.Builder automatically adds both FLAG_ONGOING_EVENT and FLAG_NO_CLEAR to the notification. This makes the notification sticky—users can't swipe it away, and it won't disappear on its own.
Update your code like this:
Notification.Builder mBuilder = new Notification.Builder(NoteActivity.this); mBuilder.setSmallIcon(R.drawable.ic_assignment_late_black); mBuilder.setContentTitle("Alert!"); mBuilder.setContentText(notess); mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); // Add this line to make the notification permanent mBuilder.setOngoing(true); // Keep your existing setAutoCancel(false) to prevent dismissal on click mBuilder.setAutoCancel(false); Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); mBuilder.setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // For API 26+, you need to create a NotificationChannel first (see note below) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("PERMANENT_CHANNEL_ID", "Permanent Notifications", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel for sticky alerts"); notificationManager.createNotificationChannel(channel); mBuilder.setChannelId("PERMANENT_CHANNEL_ID"); } notificationManager.notify(0, mBuilder.build());
2. Manually Set FLAG_NO_CLEAR (Legacy Approach)
If you need to directly manipulate the notification flags (not recommended for new code), you can do so after building the notification object:
Notification notification = mBuilder.build(); notification.flags |= Notification.FLAG_NO_CLEAR; notificationManager.notify(0, notification);
Again, using setOngoing(true) is better because it handles multiple flags and is more future-proof.
Critical Note for Android Oreo (API 26+)
Starting from Android 8.0, all notifications must belong to a NotificationChannel. If you skip creating a channel, your notification won't show up. The code above includes a channel creation block—make sure to use a unique channel ID and name for your app.
That should solve your problem! The notification will now stay visible until you explicitly cancel it with notificationManager.cancel(0).
内容的提问来源于stack exchange,提问作者Cykoč -

