如何更改Android应用程序的通知设置?

huangapple go评论59阅读模式
英文:

How to change the notification settings in android apps?

问题

以下是您要翻译的内容:

"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."

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.

1: 如何更改Android应用程序的通知设置?

英文:

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.

如何更改Android应用程序的通知设置?

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?

答案1

得分: 1

默认情况下,当您安装一个应用程序时,系统会注册一个默认的通知通道(优先级较低),默认情况下不支持悬浮通知,它是关闭的。您无法控制这一点。

但是您可以创建一个具有最高优先级的自定义通知通道,然后在应用程序运行时注册它一次。

之后,只需将通道 ID 与您的通知构建器一起传递,以便系统显示您想要的悬浮通知。

更多信息可以在这里找到:https://developer.android.com/training/notify-user/channels

英文:

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

答案2

得分: 1

以下是代码的中文翻译部分:

// 打开应用的通知设置。
private void openNotificationSettingsForApp(String channelId) {
    // 链接到此应用的通知设置。
    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);
}
英文:

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);
    }

</details>



huangapple
  • 本文由 发表于 2020年8月11日 00:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63344507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定