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?

Keivan.k
  • 548
  • 6
  • 21

2 Answers2

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

OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • 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 – Keivan.k Aug 22 '20 at 05:48
  • You're only creating a notification with highest priority payload but the system is selecting the default notification channel which buy default is on low mode. First you need to create a custom notification channel and then you need to set that notification channel as your default channel for the notification in your android manifest file. Kindly care to read when someone give solution and try atleast. – OMi Shah Aug 22 '20 at 08:15
  • How do you declare it in the manifest? I don't completely understand your solution, because when I check the notification settings, it shows that it's priority is set to high. But still no heads-up notification. – Keivan.k Aug 22 '20 at 10:50
  • based on this (https://stackoverflow.com/questions/46591282/how-do-you-specify-a-default-notification-channel-id-in-the-application-manifest) you can only set default channel for Firebase channel, not regular notifications. – Keivan.k Aug 22 '20 at 10:55
  • Then you can simply pass the channel ID with your notification builder. – OMi Shah Aug 22 '20 at 10:58
  • That's what I did. Check the 3rd line from the end!! – Keivan.k Aug 22 '20 at 11:05
  • Dude sorry but I can't help you understand when you don't want to understand. I hope somebody could help you. – OMi Shah Aug 22 '20 at 11:11
  • 1
    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. – Keivan.k Aug 22 '20 at 11:14
  • 1
    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 – Keivan.k Aug 22 '20 at 11:19
  • 1
    I'll try to give you an example with a complete project asap – OMi Shah Aug 22 '20 at 11:27
  • 1
    That would be great. Thanks. – Keivan.k Aug 22 '20 at 11:34
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);
    }
sai Pavan Kumar
  • 1,129
  • 12
  • 20
  • Thanks. it's a good alternative. I also couldn't find any solution. However, when you install Whatsapp they are all switched on. – Keivan.k Aug 23 '20 at 10:05
  • when you are creating the notification channel for your notification you can enable those according to the priority of the notification – sai Pavan Kumar Aug 23 '20 at 10:12
  • How? There is no properties in the documentation that is supposed to enable those settings. – Keivan.k Aug 23 '20 at 16:05
  • see this https://developer.android.com/guide/topics/ui/notifiers/notifications#importance and also see these constants https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT – sai Pavan Kumar Aug 24 '20 at 09:04