Wednesday, 12 April 2023

How to legally prevent notification get removed in Android

I'm working on an app and want to provide a shortcut of a function to user by adding a ongoing notification. It's a little bit like some of dictionary app doing that they provide a shortcut as notification for quick search.
The problem is even I have setOngoing(true) and setAutoCancel(false), it's still get removed once the app is closed from multitasking pane The code I was using as below .

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "C1";
            String description = "C1 is C1";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }



NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("XXXX")
                .setContentText("XXXXXXXX")
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent)
                .setOngoing(true) 
                .setAutoCancel(false);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(MainActivity.this);

        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(NOTIFICATION_ID_2, builder.build());


from How to legally prevent notification get removed in Android

No comments:

Post a Comment