首页 > 解决方案 > 如何更改 Android 应用程序中的通知设置?

问题描述

如何以编程方式打开所有这些设置?

我注意到当您安装 WhatsApp 时,它们一开始都是打开的(请看下图)。

但我找不到以编程方式打开它们的方法。

在此处输入图像描述

这是我发送通知的方式:

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


}

我还将此权限添加到清单中:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

更新: 在模拟器上使用此代码,我会收到提示通知。但在我的小米设备上,没有提示通知。它只出现在状态栏上。如果我手动打开浮动通知(您可以在照片中看到),那么我会收到提醒通知。但默认情况下它们是关闭的。当您安装 Whatsapp 时,它们都已打开。这是 Whatsapp 的一种特权,因为它很出名吗?还是有办法做到这一点?

标签: javaandroid

解决方案


我已经尝试过,但无法以编程方式设置这些设置。而不是这个,我使用以下方法打开通知设置屏幕以启用通知声音/振动。

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

推荐阅读