6

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.

enter image description here

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?

10
  • stackoverflow.com/q/42597698/9365212 this might help you Commented Aug 16, 2020 at 10:06
  • Thank, unfortunately there is no accepted answer in that thread Commented Aug 16, 2020 at 13:45
  • @Keivan.k Hi sir, have you solve the problem? Commented Aug 19, 2020 at 10:01
  • 1
    Hi Dear. Unfortunately not Commented Aug 19, 2020 at 16:02
  • @Keivan.k this question is not enough attention, we need to improve it. Commented Aug 24, 2020 at 3:01

2 Answers 2

1

By default, when you install a app, the system register's default notification channel (on low priority) that doesn't support head up notification by default, it's turned off. You can't control that.

But what you can do it create your own notification channel with highest priority and then register it on app run once.

After that just pass the channel Id with your notification builder so that system shows the head's up notification which you want.

More information can be found here https://developer.android.com/training/notify-user/channels

Sign up to request clarification or add additional context in comments.

11 Comments

As you can see I did create a channel Id with PRIORIRTY_HIGH but that doesn't work on my Xiaomi device. And the question is how to set the settings the way Whatsapp does. To send headsup notification I need to have "floating notification" turned on
Your solution is not clear. You said to declare it in the manifest but you don't have a code for it. You said pass the channel Id, and that's exactly what I did in my code. Maybe you should explain your solution in a better way. Thanks for you cooperation anyway.
I read your whole answer and all of your comments carefully. None of them answer my questions. . If you can explain your solution in a clean way please do so. Otherwise let's not continue this discussion
I'll try to give you an example with a complete project asap 👏
That would be great. Thanks.
|
1

I have tried for this but unable to set these settings programmatically. Instead of this I have used following method to open notification settings screen to enable notification sounds/vibration.

 private void openNotificationSettingsForApp(String channelId) { // Links to this app's notification settings. Intent intent = new Intent(); intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && channelId!=null){ intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_CHANNEL_ID,channelId); intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName()); } intent.putExtra("app_package", getPackageName()); intent.putExtra("app_uid", getApplicationInfo().uid); startActivity(intent); } 

4 Comments

Thanks. it's a good alternative. I also couldn't find any solution. However, when you install Whatsapp they are all switched on.
when you are creating the notification channel for your notification you can enable those according to the priority of the notification
How? There is no properties in the documentation that is supposed to enable those settings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.