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

Kei*_*n.k 6 java 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);


}
Run Code Online (Sandbox Code Playgroud)

我还添加了此权限以显示:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
Run Code Online (Sandbox Code Playgroud)

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

OMi*_*hah 1

默认情况下,当您安装应用程序时,系统会注册默认不支持抬头通知的默认通知通道(低优先级),并将其关闭。你无法控制这一点。

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

之后,只需将通道 ID 传递给您的通知生成器,以便系统显示您想要的平视通知。

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