How can I switch all these settings on programmatically?
I noticed when you install WhatsApp they are all switched on in the beginning(look at the image below).
But I can not find a way to turn them on programmatically.
Here is how I send notifications:
private void sendNotification(Intent intent){ Context context = NotificationService.this; //open the activity after the notification is clicked Intent intent1 = new Intent(getApplicationContext(),MainActivity.class); PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent1, 0); Notification.Builder builder = new Notification.Builder(context) .setTicker("Notification") .setContentTitle("Important Message") .setContentText("This is an example of a push notification using a Navigation Manager") .setSmallIcon(R.drawable.ic_add) .setContentIntent(pIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); //These are necessary for the notification to pop up if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){ builder.setPriority(Notification.PRIORITY_MAX); builder.setSound(alarmSound); builder.setLights(Color.BLUE, 500, 500); } //after android O we must use notification channels if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "Your_channel_id"; NotificationChannel channel = new NotificationChannel( channelId, "Reminder to remind to review your notes", NotificationManager.IMPORTANCE_HIGH); if(alarmSound != null){ AudioAttributes att = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_NOTIFICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build(); channel.setSound(alarmSound,att); } channel.setLightColor(Color.BLUE); channel.enableVibration(true); channel.setDescription("Hello Dear friends"); //this is to test what this is channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); channel.setVibrationPattern(new long[]{300, 300, 300}); notificationManager.createNotificationChannel(channel); builder.setChannelId(channelId); } Notification notification = builder.build(); notificationManager.notify(0, notification); } I also added this permission to manifest:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> Update: Using this code on the emulator, I get the heads-up notification. But on my Xiaomi device, there is no heads-up notification. It just appears on the status bar. If I manually turn on the floating notification (which you can see in the photo) then I will get heads-up notification. But they are switched off by default. When you install Whatsapp they are all switched on. Is that a kind of privilege for Whatsapp as it is famout? or is there a way to do it?
